ASP.NET MVC4 路由的配置 十种方法
1.一般來說當學習到配置的時候我像個人對Asp.Net Mvc 已經有了一定的了解了。
????? 路由是為了響應Http請求的產出物。在Mvc中Global.asax文件作出響應
???????? protected void Application_Start()
??????? {
??????????? AreaRegistration.RegisterAllAreas();
??????????? WebApiConfig.Register(GlobalConfiguration.Configuration);
??????????? FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
???????????? //響應的路由配置文件
??????????? RouteConfig.RegisterRoutes(RouteTable.Routes);
??????????? BundleConfig.RegisterBundles(BundleTable.Bundles);???
??????? }
?? //? RouteConfig 配置文件位于App_Start文件夾下
?//RegisterRoutes為 系統默認生成
??? public static void RegisterRoutes(RouteCollection routes)
??????? {
??????????? routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); //在沒有第三方控件的時候? 可有可無
??????????? routes.MapRoute(
??????????????? name: "Default", //默認路由名稱
??????????????? url: "{controller}/{action}/{id}", //路徑參數???? 控制器/方法/參數
??????????????? defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }??? //默認路徑對應的? 控制器/方法/參數 ? id = UrlParameter.Optional? 意為默認不做指定
??????????? );
??????? }
配置路由有兩種方法(我已知的)
?? routes.Add() ;
? routes.MapRoute();
routes.Add() ;是 實例方法
routes.MapRoute() 是RouteCollection的擴展方法
我這里采用的是routes.MapRoute() 方法 使用Add來調用比較復雜
public static void RegisterRoutes(RouteCollection routes){routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); //可有可無,默認存在的// 《一》---------------------------------------------- ----------------------- ----------------------- -----------------------// routes.MapRoute(// name: "Default",// url: "{controller}/{action}/{id}",// defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },// constraints: new// {// id = @"^\d*$", //使用正則使參數具體化// httpMethod = new HttpMethodConstraint("get")// },// 限制訪問方法// namespaces: new[] { "MVC.Controllers" } //防止有重復 Home 與 Index 出現//);//<二>---------------------------------------------- ----------------------- ----------------------- -----------------------// routes.MapRoute("", "XM{controller}/{action}", new { controller = "Home", action = "Index" }, namespaces: new[] { "MVC.Controllers" }); //訪問路徑前加上 靜態字段//《三》---------------------------------------------- ----------------------- ----------------------- -----------------------// routes.MapRoute("", "Home/{action}", new { controller = "Admin" }, namespaces: new[] { "MVC.Controllers" }); //更改文件不更改 訪問名稱//<四>---------------------------------------------- ----------------------- ----------------------- -----------------------// routes.MapRoute("", "Home/{action}", new { controller = "Admin", action = "Index" }, namespaces: new[] { "MVC.Controllers" });//<五>---------------------------------------------- ----------------------- ----------------------- -----------------------//routes.MapRoute("MyRoute", "{controller}/{action}/{id}",//MyRoute 以自己命名為準,// new// {// controller = "Home",// action = "Index",// id = "DefaultId" //也可以賦值為默認值 UrlParameter.Optional// });//給路徑定義參數默認值 //取參數在該方法下面使用 Route.data["id"] 來取值 ,同樣也可以參數取值 (string id)//<六>---------------------------------------------- ----------------------- ----------------------- -----------------------// routes.MapRoute("MyRoute", "{controller}/{action}/{id}/{*catchall}", new { controller = "Home", action = "Index", id = UrlParameter.Optional }); //*catchall 可以·獲取·參數id 后面的所有“參數”/或“路徑”//《七》---------------------------------------------- ----------------------- ----------------------- -----------------------//routes.MapRoute("MyRoute", "{controller}/{action}/{id}", // new { controller = "Home", action = "Index", id = UrlParameter.Optional },// new { controller = "^H.*", action = "^Index$|^About$" } );//使用正則 配置路由 { controller = "^H.*", action = "^Index$|^About$" } //controller 以H打頭,action 只能是 Index);//<八>---------------------------------------------- ----------------------- ----------------------- -----------------------// routes.MapRoute("", "Home/{action}", //new { controller = "Admin", action = "Index", httpMethod = new HttpMethodConstraint("get") },//限制訪問方法 httpMethod = new HttpMethodConstraint("get") //namespaces: new[] { "MVC.Controllers" }); //《九》自定義路由限制-------------------------- ----------------------- ----------------------- -----------------------////1.第一種 直接在文件中配置完成,參數添加Chrome//routes.MapRoute("", "{controller}/{action}",// new { controller = "Home", action = "Index" },// new { customConstraint = new MyRouteConstraint() }, namespaces: new[] { "MVC.Controllers" } //引入命名空間限制只有谷歌允許訪問 添加配置文件 Filters /MyRouteConstraint 繼承IRouteConstraint 必須實現方法 Match// );////2.第二種動態配置 在RouteConfig中//routes.MapRoute("", "{controller}/{action}",// new { controller = "Home", action = "Index1" },// new { customConstraint = new mySzlConstraint("Firefox") }, namespaces: new[] { "MVC.Controllers" } //引入命名空間限制只有火狐允許訪問 添加配置文件 Filters/mySzlConstraint 繼承IRouteConstraint 必須實現方法 Match// );//routes.MapRoute("", "{controller}/{action}",// new { controller = "Admin", action = "Index" },// namespaces: new[] { "MVC.Controllers" } //引入命名空間限制只有谷歌允許訪問// );//《第十種》添加靜態頁面掩飾掉 真正的路由訪問路徑------------添加文件Commom/myPage-------------- ----------------------- ----------------------- -----------------------//一.////1.設置 routes.RouteExistingFiles = true;//routes.RouteExistingFiles = true;////2.刪除 IIS 配置文件,Control+F找到UrlRoutingModule-4.0,將這個節點的preCondition屬性改為空//routes.MapRoute("myPage", "Commom/myPage.html",// new { controller = "Home", action = "Index", }, namespaces: new[] { "MVC.Controllers" });//二//1.添加配置 routes.RouteExistingFiles = true;//2.設置 routes.IgnoreRouteroutes.IgnoreRoute("Commom/{file}.html");routes.MapRoute("", "Commom/myPage.html",new { controller = "Home", action = "Index5", });}外部添加的文件
《1》
?public class MyRouteConstraint : IRouteConstraint
??? {
??????? public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
??????? {
??????????? return httpContext.Request.UserAgent.Contains("Chrome");
??????? }
??? }
《2》
?public class mySzlConstraint : IRouteConstraint
??? {
??????? private string myParam;//定義自己的參數 自己可訪問
??????? public mySzlConstraint(string agentParam)
??????? {
??????????? myParam = agentParam;? //構造函數
??????? }
?????? /// <summary>
?????? /// 實現 方法Match
?????? /// </summary>
?????? /// <param name="httpContext"> 一個數據上下文</param>
??????? /// <param name="route">定義路由</param>
?????? /// <param name="parameterName"> </param>
?????? /// <param name="values"></param>
?????? /// <param name="routeDirection"></param>
?????? /// <returns></returns>
??????? public bool Match(HttpContextBase httpContext, Route route, string parameterName,
??????????? RouteValueDictionary values, RouteDirection routeDirection)
??????? {
??????????? return httpContext.Request.UserAgent != null
??????????????? && httpContext.Request.UserAgent.Contains(myParam);
??????? }
?
轉載于:https://www.cnblogs.com/szlblog/articles/6180317.html
總結
以上是生活随笔為你收集整理的ASP.NET MVC4 路由的配置 十种方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spark Executor内幕
- 下一篇: 【译文】MySQL InnoDB 事物模