原文地址: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)