Simple command handler in .net

I want to introduce a simple command handler pattern in order to separate commands and queries of your applications, if it’s needed. Sometimes specially in large scale applications you need to have two layers for read and write separated and assign two teams to work on them.

You can use the pattern in this post based on complexity and needs of your application. I recommend that don’t call this CQRS pattern as it’s something completely different and more complex.

 

Get started and Create an interface with no any member:... Continue

Handling custom error in mvc

In asp.net mvc there is an custom error in web.config like below:

  1. <customErrors mode="On">
  2.  <error statusCode="404" redirect="~/404.html"/>
  3.  </customErrors>

Sometimes you want to redirect the user to your own custom error controller and log the error message. You can handle it! Firstly create your own Error controller:

  1.  public class ErrorController : Controller
  2.     {
  3.         public A... Continue

Call Asp.net Web API from C#

If you search in the web for calling web API from C# Code not from URL, you will find lots of sample on get and hardly enough on POST! I don't know why but I want to share with you a simple example. As a usual open visual studio and create a web API project from MVC project template.

Suppose these two simple post and get API actions:

  1.         [HttpPost]
  2.         [Route("api/Test/PostValue")]
  3.         public IHttpActionResult Po... Continue

Multiple submit buttons in asp.net MVC

In asp.net MVC you can submit a form by @using(Html.BeginForm()). You can Post the model you have binded via this operation and command the begin form to post to a Post action:   @using(Html.BeginForm("Create","FooController")).

Now if you have more than one submit button to send the page content to different post action, there are few ways: one of them is to post the content to a specific action and then decide in it to redirect which action:

 Razor:

  1. <input type="submit" value="Field1" name=&quo... Continue

Why you can't project mapped entity in entity framework

If you have worked with entity framework for a long time with and linq of course, you probably have seen the Error: The entity cannot be constructed in a LINQ to Entities query. So when it happens and why? Think about the following peace of code:

  1. public IQueryable<Lesson> GetAllLessons(int termId)
  2. {
  3.     return from p in db.Lessons
  4.            where p.termId == termId
  5.            select ... Continue

Explicit interface in C#

You have used millions of times, but you maybe don’t know what they call it (I just saw it and I didn't know too). Suppose these two interfaces with a member Called WithDraw():

 

  1.  public interface IBankingA
  2.     {
  3.         void WithDraw();
  4.     }
  5.  
  6.     public interface IBankingB
  7.     {
  8.         void WithDraw();
  9.     }

Now If you w... Continue

Introducing book: Pro Asp.net 4.5 in C#

About two years ago, I read some chapters of Pro asp.net 3.5 (it was the second edition of the book). I remember that I didn't dig in some chapter like lifecycle and Context, Modules, Handlers, Caching, Managing paths, Configurations, …  but now as I really need to know what's exactly going on the behind of asp.net I've decided to read this book in detail. In my opinion it's necessary to every asp.net developer as it really convers more about asp.net and it's frameworks. 

This book is in my books queue and will start reading of... Continue

How to turn off the caches in asp.net MVC

I've written about different kinds of caching in asp.net MVC here, I just wanted to turn off some specific caches in my project and found a way to do that, you can do that by overriding OnResultExecuting:

  1. public class TurnOffCache : ActionFilterAttribute
  2. {
  3.     public override void OnResultExecuting(ResultExecutingContext filterContext)
  4.     {
  5.         filterContext.HttpContext.Response.Cache.SetE... Continue

Why brackets are required for try/catch in C# ?

Have you ever think about that!? You don’t need to put bracket statement of if/else condition if your statement is not multiline. Needless to say, if you want to put if/else condition with just one line statement, it would be something like this:

  1.             if (a == b)
  2.                 Console.WriteLine("");
  3.             else
  4.                 Console.Write... Continue

Attribute routing with asp.net MVC5

Until the latest version of asp.net (asp.net mvc4), routing was handling by Convention-based routing to matches a URI to an Action in a separate class (RouteConfig) registered in global.asax. but in asp.net MVC5  you can use attribute routing. It means you can use attribute to set routes to your actions. It's too easy getting started with attribute routing, you just need to refer RouteConfing.cs in App_Start folder and add  routes.MapMvcAttributeRoutes(); in RegisterRoutes:

  1. &... Continue