MVC中url路由规则
Routing:首先获取视图页面传过来的请求,并接受url路径中的controller和action以及参数数据,根据规则将识别出来的数据传递给某controller中的某个action方法
MapRoute()有6个方法可以重载
方法1:系统提供的默认路由规则格式
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 }//参数默认值
);
}
url格式为:http://localhost:0000/Home/Index 对于规则为{controller}/{action}/{id} 黑色部分就是对应部分
详细匹配为:http://localhost:0000/Home/Index/1 通过Routing组件分析该url controller为Home action为Index 参数为Id为1 使用了MapRoute( string name, string url, object defaults);这个方法的重载。
注:此时若index方法中参数名称定义为Id,可以这样写:http://localhost:0000/Home/Index/1;若参数名称定义为pid,则写法必须写成http://localhost:0000/Home/Index/?pid=1
方法:2:不使用默认值的url路由规则
函数头:MapRoute( string name, string url);
routes.MapRoute("没有默认值路由规则", "{controller}/{id}-{action}");
适合的Url例子:http://localhost:0000/Custom/1-Detials
它将不匹配http://localhost:0000/
方法3:带有命名空间的Url路由规则
函数头:MapRoute( string name, string url, string[] namespaces);//路由名,Url规则,名称空间
routes.MapRoute(
"myurl",//路由名称
"{controller}/{id}-{action}",//带有参数的url
new{controller="Home",action="Index",id=UrlParameter.Optional},//参数默认值
new string[]{"mymvc.Controllers"}//命名空间
);
Url:http://localhost:0000/Custom/1-Detials
这个例子是带命名空间的路由规则,这在Aeras使用时非常有用。
方法4:带有约束的路由规则
函数头:MapRoute( string name, string url, object defaults, object constraints);//路由名,Url规则,默认值,名称空间
routes.MapRoute(
"Rule1",
"{controller}/{action}-{Year}-{Month}-{Day}}",
new { controller = "Home", action = "Index", Year = "2010", Month =
"04", Day = "21" },
new { Year = @"^\d{4}", Month = @"\d{2}" }
);
Url:http://localhost:14039/home/index-2010-01-21
方法5:带有命名空间,约束,待默认值的路由规则
函数头:MapRoute( string name, string url, object defaults, object constraints, string[] namespaces);
routes.MapRoute(
"Rule1",
"Admin/{controller}/{action}-{Year}-{Month}-{Day}",
new { controller = "Home", action = "Index", Year = "2010", Month =
"04", Day = "21" },
new { Year = @"^\d{4}", Month = @"\d{2}" },
new string[] { "MvcDemo.Controllers" }
);
Url:http://localhost:14039/Admin/home/index-2010-01-21
方法6:捕获所有的路由
routes.MapRoute(
"All", // 路由名称
"{*Vauler}", // 带有参数的 URL
new { controller = "Home", action = "Index", id =
UrlParameter.Optional } // 参数默认值
);
关于Global.asax剩余部分的说明:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");是忽略这个规则的Url
AreaRegistration.RegisterAllAreas();//注册所有的Areas
RegisterRoutes(RouteTable.Routes);//注册我们写的规则
//RouteDebug.RouteDebugger.RewriteRoutesForTesting(RouteTable.Routes);//调试用语句,需要下载RouteDebug.dll,并添加引用!加入这句话后就可以测试Url路由了。
最新文章
- firebug不能加载JS文件 ,无法进行JS脚本调试
- angularjs结合d3js实现资源展示
- 支持向量机 (SVM)分类器原理分析与基本应用
- sql 中 left join 的使用
- 【GoLang】GoLang 错误处理 -- 使用 error is value 的思路处理,检查并处理error
- 51nod 1211 数独
- Eclipse 和 NetBeans 快捷键即其他常用功能比较
- Android Studio常用快捷键汇总(mac)
- 利用Azure高级存储搭建高性能Linux服务器(2)
- hdu 3911 Black And White(线段树)
- FZU 2147 A-B Game(数学)
- 2017春 前端自动化(二) 页面自动刷新、sass与css转换的使用、pxToRem直观转换
- 使用Dism备份和全新恢复系统
- HashMap/HashSet,hashCode,哈希表
- 简单记录numpy库的某些基本功能
- ArcGIS JS API多线程克里金插值
- python编程之如何在Windows上安装python
- android -------- 混淆打包报错(warning - InnerClass annotations are missing corresponding EnclosingMember annotations)
- Java标准I/O流编程一览笔录
- 关于 wsdl2Java 自动生成客户端调取webservice接口