ASP.NET MVC Best Practices 27+
12 ASP.NET MVC Best Practices
M
DomainModel != ViewModel
Use ActionFilters for “shared” data
V
Do NEVER user code-behind
Write HTML each time you can
If there is an if, write an HtmlHelper
Choose your view engine carefully
C
Delete the AccountController
Isolate Controllers from the outside World
Use an IoC Container
Say NO to “magic strings”
Build your own “personal conventions”
Pay attention to the Verbs
ASP.NET MVC Best Practices
Use
Use jQuery and jQuery UI
Use Bootstrapper in Global.asax
Use Action Filter to Convert to compatible Action Methods parameters
Use UpdateModel Carefully
Use PRG Pattern for Data Modification
Do Not
Do not make any hard dependency on the DI Container, use Common Service Locator
Do not put your JavaScript codes in your View
Avoid ViewData, use ViewData.Model
Do not make any hard dependency on the DI Container, use Common Service Locator
Controller will not contain any Domain logic
Decorate
Decorate your most frequent Action Methods with OutputCache Attribute
Decorate your Action Methods with Proper AcceptVerbs Attribute
Decorate your most frequent Action Methods with OutputCache Attribute
Decorate your Action Methods with Proper AcceptVerbs Attribute
Create
Create new ActionResult if required
Create Extension methods of UrlHelper to generate your url from Route
Create Layer Super Type for your ViewModel and Use Action Filter to populate common parts.
Create Extension Method of UrlHelper to map your JavaScript, Stylesheet and Image Folder
HtmlHelper extension
Split your View into multiple ViewUserControl
Tips
Routing consideration
Encode
Action Filter Location
Keep your controller free from HttpContext and its tail
ÔÎĵØÖ·£º
http://weblogs.asp.net/rashid/archive/2009/04/01/asp-net-mvc-best-practices-part-1.aspx
http://codeclimber.net.nz/archive/2009/10/27/12-asp.net-mvc-best-practices.aspx
Ïà¹ØÎĵµ£º
ÔÌûµØÖ·:http://blog.cnria.com/post/aspnet-form-e5b58ce5a597-e68f90e4baa4e9a1b5e99da2e79a84e8a7a3e586b3e696b9e6b395-from-e5b58ce5a597-aspnet-e8a1a8e58d95.aspx
Õâ¸öÎÊÌâµÄÔÒòÆäʵ²¢²»ÊÇasp.net ÔËÐлúÖƵÄÔÒò£¬¶øÊÇÒòΪÔÚhtmlÖб¾Éí¾Í²»ÔÊÐíform±êǩǶÌ×ʹÓã¬ÎÒÊÔ¹ýdocument.getelementbyid("search&q ......
Ê×ÏÈÎÒºÜÒź¶µÄ¸æËß´ó¼Ò£¬ÒòΪ΢ÈíµÄ͵ÀÁ£¬Ä¿Ç°UpdatePanel»¹²»Ö§³ÖÎļþÉÏ´«¡£±äÏàµÄ½â¾ö°ì·¨¾ÍÊÇUpdatePanelÖÐÉèÖÃPostBackTrigger£º
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:FileUpload ID="FileUpload1" runat="serv ......
»ñÈ¡ÇëÇóµÄÒ³µÄ HttpRequest ¶ÔÏó¡£
Ò»¡¢HttpRequest Àà
ʹ ASP.NET Äܹ»¶ÁÈ¡¿Í»§¶ËÔÚ Web ÇëÇóÆڼ䷢Ë굀 HTTP Öµ¡£
ÏÂÃæµÄ´úÂëʾÀýʹÓà StreamWriter ÀཫÈô¸É HttpRequest ÀàÊôÐÔÖµµÄֵдÈëÎļþ¡£¶ÔÓÚÊÇ×Ö·û´®ÀàÐ͵ÄÊôÐÔ£¬ÊôÐÔÖµ±»Ð´ÈëÎļþʱ½«±»±àÂëΪ HTML¡£±íʾ¼¯ºÏµ ......
дͼƬc:\1.jpgµ½±ícinfoÖÐ
private static void AddCinfo()
{
string strSql = "insert into cinfo (srvtitle,csttitle,introduction,logo) values
(@srvtitle,@csttitle,@introduction,@logo)";
S ......
»ñÈ¡Óë¸Ã Page ¶ÔÏó¹ØÁªµÄ HttpResponse ¶ÔÏ󡣸öÔÏóʹÄúµÃÒÔ½« HTTP ÏìÓ¦Êý¾Ý·¢Ë͵½¿Í»§¶Ë£¬²¢°üº¬ÓйظÃÏìÓ¦µÄÐÅÏ¢¡£
Ò»¡¢HttpResponse Àà
·â×°À´×Ô ASP.NET ²Ù×÷µÄ HTTP ÏìÓ¦ÐÅÏ¢¡£
HttpResponse ÀàÐ͹«¿ªÒÔϳÉÔ±¡£
¹¹Ô캯Êý
Ãû³Æ
˵Ã÷
HttpResponse
»ù´¡½á¹¹¡£³õʼ»¯ HttpRespons ......