日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

ASP.NET MVC4 路由的配置 十种方法

發(fā)布時間:2025/5/22 asp.net 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ASP.NET MVC4 路由的配置 十种方法 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1.一般來說當學(xué)習(xí)到配置的時候我像個人對Asp.Net Mvc 已經(jīng)有了一定的了解了。

????? 路由是為了響應(yīng)Http請求的產(chǎn)出物。在Mvc中Global.asax文件作出響應(yīng)

???????? protected void Application_Start()
??????? {
??????????? AreaRegistration.RegisterAllAreas();

??????????? WebApiConfig.Register(GlobalConfiguration.Configuration);
??????????? FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);

???????????? //響應(yīng)的路由配置文件
??????????? RouteConfig.RegisterRoutes(RouteTable.Routes);


??????????? BundleConfig.RegisterBundles(BundleTable.Bundles);???
??????? }

?? //? RouteConfig 配置文件位于App_Start文件夾下

?//RegisterRoutes為 系統(tǒng)默認生成

??? public static void RegisterRoutes(RouteCollection routes)
??????? {
??????????? routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); //在沒有第三方控件的時候? 可有可無

??????????? routes.MapRoute(
??????????????? name: "Default", //默認路由名稱
??????????????? url: "{controller}/{action}/{id}", //路徑參數(shù)???? 控制器/方法/參數(shù)
??????????????? defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }??? //默認路徑對應(yīng)的? 控制器/方法/參數(shù) ? id = UrlParameter.Optional? 意為默認不做指定
??????????? );
??????? }

配置路由有兩種方法(我已知的)

?? routes.Add() ;

? routes.MapRoute();

routes.Add() ;是 實例方法

routes.MapRoute() 是RouteCollection的擴展方法

我這里采用的是routes.MapRoute() 方法 使用Add來調(diào)用比較復(fù)雜

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*$", //使用正則使參數(shù)具體化// httpMethod = new HttpMethodConstraint("get")// },// 限制訪問方法// namespaces: new[] { "MVC.Controllers" } //防止有重復(fù) Home 與 Index 出現(xiàn)//);//<二>---------------------------------------------- ----------------------- ----------------------- -----------------------// routes.MapRoute("", "XM{controller}/{action}", new { controller = "Home", action = "Index" }, namespaces: new[] { "MVC.Controllers" }); //訪問路徑前加上 靜態(tài)字段//《三》---------------------------------------------- ----------------------- ----------------------- -----------------------// 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// });//給路徑定義參數(shù)默認值 //取參數(shù)在該方法下面使用 Route.data["id"] 來取值 ,同樣也可以參數(shù)取值 (string id)//<六>---------------------------------------------- ----------------------- ----------------------- -----------------------// routes.MapRoute("MyRoute", "{controller}/{action}/{id}/{*catchall}", new { controller = "Home", action = "Index", id = UrlParameter.Optional }); //*catchall 可以·獲取·參數(shù)id 后面的所有“參數(shù)”/或“路徑”//《七》---------------------------------------------- ----------------------- ----------------------- -----------------------//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.第一種 直接在文件中配置完成,參數(shù)添加Chrome//routes.MapRoute("", "{controller}/{action}",// new { controller = "Home", action = "Index" },// new { customConstraint = new MyRouteConstraint() }, namespaces: new[] { "MVC.Controllers" } //引入命名空間限制只有谷歌允許訪問 添加配置文件 Filters /MyRouteConstraint 繼承IRouteConstraint 必須實現(xiàn)方法 Match// );////2.第二種動態(tài)配置 在RouteConfig中//routes.MapRoute("", "{controller}/{action}",// new { controller = "Home", action = "Index1" },// new { customConstraint = new mySzlConstraint("Firefox") }, namespaces: new[] { "MVC.Controllers" } //引入命名空間限制只有火狐允許訪問 添加配置文件 Filters/mySzlConstraint 繼承IRouteConstraint 必須實現(xiàn)方法 Match// );//routes.MapRoute("", "{controller}/{action}",// new { controller = "Admin", action = "Index" },// namespaces: new[] { "MVC.Controllers" } //引入命名空間限制只有谷歌允許訪問// );//《第十種》添加靜態(tài)頁面掩飾掉 真正的路由訪問路徑------------添加文件Commom/myPage-------------- ----------------------- ----------------------- -----------------------//一.////1.設(shè)置 routes.RouteExistingFiles = true;//routes.RouteExistingFiles = true;////2.刪除 IIS 配置文件,Control+F找到UrlRoutingModule-4.0,將這個節(jié)點的preCondition屬性改為空//routes.MapRoute("myPage", "Commom/myPage.html",// new { controller = "Home", action = "Index", }, namespaces: new[] { "MVC.Controllers" });////1.添加配置 routes.RouteExistingFiles = true;//2.設(shè)置 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;//定義自己的參數(shù) 自己可訪問

??????? public mySzlConstraint(string agentParam)
??????? {
??????????? myParam = agentParam;? //構(gòu)造函數(shù)
??????? }

?????? /// <summary>
?????? /// 實現(xiàn) 方法Match
?????? /// </summary>
?????? /// <param name="httpContext"> 一個數(shù)據(jù)上下文</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);
??????? }


?

轉(zhuǎn)載于:https://www.cnblogs.com/szlblog/articles/6180317.html

總結(jié)

以上是生活随笔為你收集整理的ASP.NET MVC4 路由的配置 十种方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。