ASP.NET MVC最佳實務(2)

來源:互聯網
上載者:User

原文地址:http://space.itpub.net/740297/viewspace-588774

 

2.建立UrlHelper的擴充方法(extension method)來映射您的JavaScript, Stylesheet以及Image檔案夾預設情況下ASP.NET MVC會建立Content, Scripts檔案夾來存放它們,但是我不喜歡這種方式。我喜歡以下的檔案夾組織方式,它能夠讓我僅要為一個Assets檔案夾設定靜態檔案快取,而不是為多個檔案夾分別設定:Assets +images +scripts +stylesheets無論檔案夾是按什麼方式組織的,建立UrlHelper的擴充方法來映射這些檔案夾,以便您能夠在視圖中很方便地引用它們,並且,將來如果您需要修改檔案夾的結構,您也不必做大量的“尋找/替換”。建議為那些在您的視圖中經常引用到的資源建立擴充方法。。例如:1.public static string Image(this UrlHelper helper, string fileName)  2.{  3.   return helper.Content("~/assets/images/{0}".FormatWith(fileName));  4.}  5. 6.public static string Stylesheet(this UrlHelper helper, string fileName)  7.{  8.   return helper.Content("~/assets/stylesheets/{0}".FormatWith(fileName));  9.}  10. 11.public static string NoIcon(this UrlHelper helper)  12.{  13.   return Image(helper, "noIcon.png");  14.}  在引用這些資源時,您可以這樣來引用:1.<link href="<%= Url.Stylesheet("site.css")%>" rel="stylesheet" type="text/css"/>  而不是預設的這樣:1.<link href="http://www.cnblogs.com/Content/Site.css" rel="stylesheet" type="text/css" />  

相關閱讀:

  • ASP.NET MVC Unleashed (5) (續) (geez, 2009-3-13)
  • ASP.NET MVC Unleashed (6) (geez, 2009-3-17)
  • ASP.NET MVC 1.0 正式發布 (geez, 2009-3-18)
  • ASP.NET MVC Unleashed (6) (續) (geez, 2009-3-21)
  • ASP.NET MVC技術專題 (朱先忠, 2009-3-27)
  • ASP.NET MVC筆記 之 Action 過濾器 (iDotNetSpace, 2009-4-09)
  • Asp.Net Mvc: 淺析TempData機制 (iDotNetSpace, 2009-4-09)
  • ASP.NET MVC futures: MVC控制項概述 (geez, 2009-4-09)
  • ASP.NET MVC futures: 局部視圖 (geez, 2009-4-10)
  • ASP.NET MVC最佳實務(1) (geez, 2009-4-10)

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.