12 best practices for Asp.net MVC

Source: Internet
Author: User

Last week I gave two lectures in Rome on ASP.net MVC, which was developed shortly after http://www.dotnetromacesta.org. One of the speeches was about my views on ASP.net MVC best practices. because the speech was made in Italy, I translated the PPT used in the speech into English for better reading.

Best practices for Controller
1-delete accountcontroller

Let demoCodeIn yourProgramIs a very bad practice. Never use accountcontroller.

2-isolate external networks and controllers

If you rely on httpcontext, data types, configurations, logs, and so on, it will make the program difficult to test, modify, or further develop.

3-use an IOC container

It is easier to achieve the second best practice. I use ninject V2 to manage all external dependencies using IOC containers. There are many IOC containers, and you can even implement one by yourself if needed.

4-No with "Magic strings"

Never useViewdata ["key"],Create a viewmodel for each view to use a strongly typedViewpage <viewmodel>.

The magic strings is very evil, because you may cause view errors due to misspelling, and a strong model can not only have smart awareness, in addition, errors are obtained at compilation rather than runtime.

5-create your own "personal practices"

Use Asp.net MVC as the basis for your personal (or company) reference architecture, you can also make the controller and view inherit from your own base class instead of the default base class to make your practice more thorough.

6-pay attention to verbs

Even if you do not use the most appropriate HTTP verb, you must use the PRG mode (post-redirect-Get): Use get to display data and use post to modify data.

Best practices for Model
7-domainmodel! = Viewmodel

Domainmodel represents the corresponding domain, but viewmodel is created for the view needs. The two may be different (generally). In addition, the domainmodel is a combination of data and behavior, which is composed of complex variable types and has layers. Viewmodel only consists of some simple variable types such as string. If you want to remove the ORM code that is redundant and prone to errors, you can use automapper. For more information, see ASP. net mvc view model patterns.

8-use actionfilter for "shared" Data

This is my own solution and may need to be discussed in future posts. Generally, you do not want the data obtained by your controller to be shared among several different views. My method is to use actionfilter to obtain the data shared among several different views, then use the appropriate view for display.

View Best Practices
9-do not use codebehind Mode

Never.

10-try to write HTML code

I think Web developers must be used to writing HTML (or CSS and JavaScript ). therefore, it is better to use htmlhelper (such as HTML. submit or HTML. button ). this will also be discussed in future posts.

11-if an if statement exists, use htmlhelper

View must be dumb (controller is thin and model is fat). If you find that you are using the if statement, write an htmlhelper to hide the selection condition statement.

12-carefully select your view Engine

The default engine room webformviewengine, IMHO is not the best engine. I prefer spark viewengine, because this engine is more suitable for MVC view. What I like is"Dominates the flow and that code shoshould fit seamlessly"For each loop, the IF statement is defined in the "HTML Tag.

Download DEMO code and slides

You can download slides or DEMO code.

 

 

 

 

http://codeclimber.net.nz/archive/2009/10/27/12-asp.net-mvc-best-practices.aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.