在ASP.NET MVC中,一個URL請求是由對應的一個Controller中的Action來處理的,由URL Routing來告訴MVC如何定位到正確的Controller和Action。
默認路由表
當我們在VS中創建一個新的 ASP.NET MVC程序,程序將會自動使用默認的路由表。
public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "Default",//默認路由名稱 url: "{controller}/{action}/{id}",//URL 和參數 defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }//默認參數 ); }
上面的代碼是向路由表添加一個名為(Default)的路由。
url 中
{controller} 對應的也就是控制器名稱,
{action} 對應的是控制器中的action,
{id} 對應為參數,
defaults 為默認值 controller = "Home" action = "Index" 。
當你在地址欄輸入/Home 因為action 有對應的默認值 ,程序也就會執行HomeController.Index()。
當你在地址欄輸入/Home/Index/6 時,路由會自動對應參數
controller = Home
action = Index
id = 6
程序也就會去執行 HomeController.Index(6)。
示例展示:
上面的靜態方法RegisterRoutes是在Global.asax.cs文件中的Application_Start方法中被調用的。
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Http; using System.Web.Mvc; using System.Web.Routing; namespace MvcApplication { // Note: For instructions on enabling IIS6 or IIS7 classic mode, // visit http://go.microsoft.com/?LinkId=9394801 public class MvcApplication : System.Web.HttpApplication { protected void Application_Start() { AreaRegistration.RegisterAllAreas(); WebApiConfig.Register(GlobalConfiguration.Configuration); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); } } }
同時也包含了其他的一些方法。
自定義路由表
下面增加一個自己定義的路由表
public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "Default",//默認路由名稱 url: "{controller}/{action}/{id}",//URL 和參數 defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },//默認參數 constraints: new { action = @"[a-zA-Z]+" }//只匹配字母 ); routes.MapRoute( name: "MyRoute",//路由名稱 url: "News/{newsid}",//URL不帶參數 defaults: new { controller = "News", action = "Index" },//默認指定action到Index constraints: new { newsid = @"\d+" }//newsid必須為數字 ); }
當你在地址欄輸入/News/Index?newsid=1 和 /News/1 效果是一樣的。
訪問/News/1 它會默認 訪問 controller=News action=Index 然后參數為1 。
我把默認的路由規則,加了一下限制,讓其action 只執行字母,以免覆蓋新加的路由規則。
這樣我們可以縮短URL 地址,讓URL 更加直觀友好。
路由約束:
正則表達式約束
通過正則來約束相關路由規則。
routes.MapRoute( name: "MyRoute",//路由名稱 url: "News/{newsid}",//URL不帶參數 defaults: new { controller = "News", action = "Index" },//默認指定action到Index constraints: new { newsid = @"\d+" }//newsid必須為數字 );
上面為約束 newsid為 數字。
routes.MapRoute( name: "Default",//默認路由名稱 url: "{controller}/{action}/{id}",//URL 和參數 defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },//默認參數 constraints: new { action = @"[a-zA-Z]+" }//只匹配字母 );
上面為約束 action 只為字母,不允許其他規則。
Http請求方式約束
routes.MapRoute( name: "MyRoute",//路由名稱 url: "News/{newsid}",//URL不帶參數 defaults: new { controller = "News", action = "Index" },//默認指定action到Index constraints: new { newsid = @"\d+", httpMethod = new HttpMethodConstraint("POST") }//newsid必須為數字 並且請求必須為POST );
上面為約束 http請求 為POST
自定義路由約束
如果標准的路由約束滿足不了你的需求,那么可以通過實現 IRouteConstraint 接口來定義自己的路由約束規則。
下面就是實現了一個請求是否來自本地的約束。
public class LocalhostConstraint : IRouteConstraint { public bool Match ( HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection ) { return httpContext.Request.IsLocal; } }
在路由表中的應用:
routes.MapRoute( name: "Default",//默認路由名稱 url: "{controller}/{action}/{id}",//URL 和參數 defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },//默認參數 constraints: new { action = @"[a-zA-Z]+", isLocal = new LocalhostConstraint() }//只匹配字母 並且請求只允許本地 );
上面為約束 http請求 來自本地。
如果你覺得本文對你有幫助,請點擊“推薦”,謝謝。