精通ASP.NET MVC ——属性路由
在上一篇文章中,有約定路由的介紹。MVC 5中增加了對屬性路由這一新技術的支持。在屬性路由中,路由是由直接運用于控制器的C#屬性定義的。
啟動和運用屬性路由?
默認情況下屬性路由是禁止的,通過MapMvcAttributeRoutes擴展方法可以啟用它。該方法由RouteCollection對象調用,如下圖所示:
public static void RegisterRoutes(RouteCollection routes){routes.IgnoreRoute("{resource}.axd/{*pathInfo}");routes.MapMvcAttributeRoutes();//啟動屬性路由routes.MapRoute(name: "Default",url: "{controller}/{action}/{id}",defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional });}調用?MapMvcAttributeRoutes?方法導致路由系統檢查應用系統中的控制器類,并尋找匹配路由屬性。最重要的屬性是Route。如下圖所示:
public class CustomerController : Controller{[Route("Test")]// GET: Customerpublic ActionResult Index(){ViewBag.Controller = "Customer";ViewBag.Action = "Index";return View("ActionName");}public ActionResult List(){ViewBag.Controller = "Customer";ViewBag.Action = "List";return View("ActionName");}}當一個動作方法用Route屬性修飾時,它不需要通過約定路由來訪問,這意味著Customer控制器中的Index方法不再需要/Customer/Index,也不能用/Customer/Index來訪問。?執行效果如下圖所示:
?
使用片段變量創建路由?
雖然通過屬性表示,但是屬性路由支持和基于約定路由一樣的特性。這些特性包括創建包含片段變量的路由。如下圖所示:
public class CustomerController : Controller{[Route("Test")]// GET: Customerpublic ActionResult Index(){ViewBag.Controller = "Customer";ViewBag.Action = "Index";return View("ActionName");}[Route("Users/Add/{user}/{id}")]public string Create(string user, int id){return string.Format("User:{0},ID:{1}",user,id);}public ActionResult List(){ViewBag.Controller = "Customer";ViewBag.Action = "List";return View("ActionName");}}上述代碼中添加了一個帶有string 和 int 類型參數的動作方法Create。為了簡單起見,返回一個string類型的結果,這樣就不用創建一個視圖了。?用Route屬性定義的路由混合了靜態前綴(Users/Add)以及方法參數對應的user 和 id 片段。為了調用Create方法,MVC框架使用模型綁定特性將片段變量轉化成正確的類型。運行效果如下圖所示:
運用路由約束?
屬性路由中的約束更加直接,如下圖所示:
public class CustomerController : Controller{[Route("Test")]// GET: Customerpublic ActionResult Index(){ViewBag.Controller = "Customer";ViewBag.Action = "Index";return View("ActionName");}[Route("Users/Add/{user}/{id:int}")]public string Create(string user, int id){return string.Format("User:{0},ID:{1}",user,id);}[Route("Users/Add/{user}/{password}")]public string ChangePass(string user, string password){return string.Format("changePass Method - User:{0}, Pass:{1}",user,password);}public ActionResult List(){ViewBag.Controller = "Customer";ViewBag.Action = "List";return View("ActionName");}}新的動作方法名稱為ChangePass,有兩個string類型的參數。但是為了區分Create方法,對Create方法中的Route屬性運用了約束,[Route("Users/Add/{user}/{id:int}")],指定 id 接收int類型的請求。?運行效果如下圖所示:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
組合約束?
可以將多個約束運用于一個片段,以便進一步限制路由將要匹配的值的范圍,如下圖所示:
[Route("Users/Add/{user}/{password:alpha:length(6)}")]public string ChangePass(string user, string password){return string.Format("changePass Method - User:{0}, Pass:{1}",user,password);}?使用Route Prefix
可以使用Route Prefix屬性定義一個將被運用到控制器所有路由中的普通前綴,當很多動作都使用同樣的根URL為目標是,這點特別有用。如下圖所示:
[RoutePrefix("Users")]public class CustomerController : Controller{[Route("~/Test")]// GET: Customerpublic ActionResult Index(){ViewBag.Controller = "Customer";ViewBag.Action = "Index";return View("ActionName");}[Route("Add/{user}/{id:int}")]public string Create(string user, int id){return string.Format("User:{0},ID:{1}",user,id);}[Route("Add/{user}/{password:alpha:length(6)}")]public string ChangePass(string user, string password){return string.Format("changePass Method - User:{0}, Pass:{1}",user,password);}public ActionResult List(){ViewBag.Controller = "Customer";ViewBag.Action = "List";return View("ActionName");}}運行效果如下圖所示:
? ? ? ? ? ? ? ? ? ? ???
以~/為前綴告訴MVC框架,不要運用到Index動作方法的RoutePrefix屬性,這意味著它仍然可以通過/Test來訪問。?
?
?
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的精通ASP.NET MVC ——属性路由的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 题解CF110E
- 下一篇: 隐藏ASP.NET MVC版本