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 路由的配置 十种方法相关推荐
- Mvc多级Views目录 asp.net mvc4 路由重写及 修改view 的寻找视图的规则
一般我们在mvc开发过程中,都会碰到这样的问题.页面总是写在Views文件夹下,而且还只能一个Controller的页面只能写在相应的以Controller名命名的文件夹下.如果我们写到别处呢?那么肯 ...
- ASP.NET MVC ETag Cache等优化方法
背景 最近有一个项目是用SmartAdmin + Jquery + EasyUI 一个ASP.NET MVC5的项目,一直存在一个性能问题,加载速度比较慢,第一次加载需要(在没有cache的情况下)需 ...
- ASP.NET MVC4中调用WEB API的四个方法
当今的软件开发中,设计软件的服务并将其通过网络对外发布,让各种客户端去使用服务已经是十分普遍的做法.就.NET而言,目前提供了Remoting,WebService和WCF服务,这都能开发出功能十分强 ...
- ASP.NET MVC4中@model使用多个类型实例的方法
有时需要在ASP.NET MVC4的视图的@model中使用多个类型的实例,.NET Framework 4.0版本引入的System.Tuple类可以轻松满足这个需求. 假设Person和Produ ...
- ASP.NET Core启动地址配置方法及优先级顺序 | .NET 6 版本
前言 上次,我们讨论了如何通过配置或代码方式修改启动地址:<ASP.NET Core启动地址配置方法及优先级顺序>.不过是基于 .NET 5 版本的. 由于 .NET 6 使用了最小 WE ...
- asp.net core webapi项目配置全局路由
一.前言 在开发项目的过程中,我新创建了一个controller,发现vs会给我们直接在controller头添加前缀,比如[Route("api/[controller]")], ...
- [转]ASP.NET MVC4中@model使用多个类型实例的方法
本文转自:http://blog.csdn.net/hulihui/article/details/48199897 有时需要在ASP.NET MVC4的视图的@model中使用多个类型的实例,.NE ...
- mvc4 html.dropdownlist,ASP.NET MVC4中使用Html.DropDownListFor的方法示例
本文实例讲述了ASP.NET MVC4中使用Html.DropDownListFor的方法.分享给大家供大家参考,具体如下: 一.控制器部分: public ActionResult PageDeta ...
- mvc html.dropdownlist,ASP.NET MVC4中使用Html.DropDownListFor的方法示例
本文实例讲述了ASP.NET MVC4中使用Html.DropDownListFor的方法.分享给大家供大家参考,具体如下: 一.控制器部分: public ActionResult PageDeta ...
最新文章
- docker环境无法执行jmap -heap 56命令
- dofuscator C# 混淆器 原来如此
- 手机耗电统计app_教你 6 招,解决 iOS13 耗电问题
- Spring框架的前世今生以及对Spring的宏观认识
- Nginx添加ngx_http_headers_module模块
- Linux下动态库使用小结
- python zip压缩_Python zip压缩与解压(zipfile模块实例)
- 便携式计算机推销洽谈方案,推销洽谈的方法
- 软件需求说明书谁负责写_新手入门软件测试需要掌握哪些知识点?
- angular HttpClient getbyid 方法获取数据
- 计算机视觉实战(十四)答题卡识别 (附完整代码)
- 计时器ajax,使用AJAX请求计时器
- 【DIY】通达信DIY添加扫雷宝、地图和复盘(1)
- img标签,图片占位
- Couch-to-5K 跑步计划
- Cannot find current proxy: Set 'exposeProxy' property on Advised to 'true' to make it available
- 基于51单片机的智能窗帘设计
- adb bugreport -- Failed to get bugreportz version
- Portfolio、program和project的联系和区别;它们与operation和OPM的关系
- 服务器配置地址不正确,网络连接配置不正确IP地址是否正确网络存在问题..._网络编辑_帮考网...