ASP.NET MVC Best Practices

ASP.NET MVC is one of the best web development platforms that Microsoft offers. There are several learning resources available online for learning the  ASP.NET MVC to build your web applications. This blog is for those who are already familiar with the framework. In this article, I will explain all the best practices that will make your work in ASP.NET MVC framework more easy.

First, let's discuss the basics of MVC(Model, View Controller) design pattern.

So, why do we actually need it?
User Interface contains a lot of code. Because of the complicated logic, it needs to be handled. MVC consists of three components - Model(Data Layer), View(Presentation Layer) and Controller(Business Logic Layer). The Golden Rule that MVC pattern follows is Separation of Concerns(SoC).

Controller Best Practices
  1. Decouple your Controllers
    Dependencies of the Controller class on Data Access Logic, Web Services etc. makes controller logic difficult to understand. Use an IOC container like Ninject to reduce these dependencies.

  2. Use PRG Pattern
    Use Post-Redirect-Get Pattern in order to use correct Http verbs for each Action. For example, GET verb to show data and POST to modify.

  3. Create Slim Controller
    Controllers should contain much less code. Ideally, Controllers are designed for routing the user request to the right business logic component and returning the right View .

  4. Always Use ViewModels
    Always try to use ViewModel instead of dynamic ViewData dictionary. ViewData can become a nightmare because they cannot tell you the reason why the View is failing at the time of compilation.

  5. Delete Demo Code Files
    You will not use demo code files provided with ASP.NET MVC project, like AccountController. So, delete them in the very beginning to make your application structure neat and clean.

Model Best Practices

  1. Don’t Use Controllers For Shared Data
    If you have some data that needs to be shared across multiple Views, never use Controllers to access this data. Instead, you should use ActionFilters to access shared data.

  2. View Models are different from Domain Models
    View Models just contain the properties that are related to the View while Domain Model represents the domain. Try to use Auto Mappers to build your View Models.

View Best Practices

  1. Say No to Business Logic
    Views should not contain any business logic and must be clean. All business logics must be performed inside the controllers.

  2. No JavaScript in Views
    Don’t place your JavaScript code inside the Views. Create separate script files for JavaScript.

  3. Write an HtmlHelper for Conditions
    If you find yourself writing an if statement, write an HtmlHelper for it in order to hide if else code from the View.

Other Best Practices

  1. Cache Your Data
    In order to reduce the bandwidth consumption and increase application performance, you can use caching to store the data in memory, so that the application doesn't need to generate the same content every time when the same action is called.

  2. Bundling and Minification
    Calling multiple resources, like CSS files, from one html page can hurt your application performance severely. Bundling groups all of these resources in one downloadable resource. Minification can also accelerate your application performance by removing all unnecessary characters, white-spaces, and comments from CSS files and scripts.