Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best Practices for ASP.NET MVC [closed]

Tags:

asp.net-mvc

Use T4MVC to eliminate all magic strings and magic anonymous types in your entire project. This will help you in refactoring later in your project, and (apart from having to still ensure your routes are well-defined) all Action calls get their proper parameters. It changes calls like this:

<%= Html.ActionLink("Link text", "Products", "Details", new { id = Model.Id }) %>

Into:

<%= Html.ActionLink("Link text", MVC.Products.Details(Model.Id)) %>

Keep any and all logic out of your view. Your controller should determine what gets shown - your view should be dumb.


  1. IoC/DI for Controller factory (so I can inject IRepository, ISomeService in controllers constructor)
  2. never access HttpContext directly, build wrapper, so it can be unit tested
  3. Validation framework for model binding validations (xVal or FluentValidation). Built-in validation inside MVC 1 is basic
  4. never use "magic strings": for calling controllers/actions from View, for RouteLink, RenderPartial, RenderAction, ...
  5. never use ViewData, build DTO ViewModel classes. Use AutoMapper for mapping data from domain entities to ViewModel DTO objects for View

ViewModel DTO objects:
BaseViewModel abstract class, with properties for rendering page meta data, menus and all other stuff that appears on every page. All other ViewModel classes inherits from BaseViewModel.


While not a best practices site, the below site is doing great things with asp.net MVC that could probably be considered best practice.

http://www.codeplex.com/MVCContrib