原文地址:http://space.itpub.net/740297/viewspace-586997
1.建立UrlHelper類的擴充方法,產生相對路徑URL請避免將控制器、行為、或者路由名稱作為字串到處傳遞,建立UrlHelper的擴充方法來封裝它們,例如:1.public static class UrlHelperExtension 2.{ 3. public static string Home(this UrlHelper helper) 4. { 5. return helper.Content("~/"); 6. } 7. 8. public static string SignUp(this UrlHelper helper) 9. { 10. return helper.RouteUrl("Signup"); 11. } 12. 13. public static string Dashboard(this UrlHelper helper) 14. { 15. return Dashboard(helper, StoryListTab.Unread); 16. } 17. 18. public static string Dashboard(this UrlHelper helper, StoryListTab tab) 19. { 20. return Dashboard(helper, tab, OrderBy.CreatedAtDescending, 1); 21. } 22. 23. public static string Dashboard(this UrlHelper helper, StoryListTab tab, OrderBy orderBy, int page) 24. { 25. return helper.RouteUrl("Dashboard", new { tab = tab.ToString(), rderBy = orderBy.ToString(), page }); 26. } 27. 28. public static string Update(this UrlHelper helper) 29. { 30. return helper.RouteUrl("Update"); 31. } 32. 33. public static string Submit(this UrlHelper helper) 34. { 35. return helper.RouteUrl("Submit"); 36. } 37.} 這樣的話,您就可以在視圖中這樣來使用:1.<a href="<%= Url.Dashboard() %>">Dashboard</a> 2.<a href="<%= Url.Profile() %>">Profile</a> 而不是這樣:1.<%= Html.ActionLink("Dashboard", "Dashboard", "Story") %> 2.<a href="<%= Url.RouteUrl("Profile")%>">Profile</a> 並且在控制器中我能這麼用:1.return Redirect(Url.Dashboard(StoryListTab.Favorite, OrderBy.CreatedAtAscending, 1)) 而不是這樣:1.return RedirectToAction("Dashboard", "Story", new { tab = StoryListTab.Favorite, rderBy = OrderBy.CreatedAtAscending, page = 1 });
相關閱讀:
- ASP.NET MVC Unleashed (5) (geez, 2009-3-12)
- 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)