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路由了。

最新文章

  1. firebug不能加载JS文件 ,无法进行JS脚本调试
  2. angularjs结合d3js实现资源展示
  3. 支持向量机 (SVM)分类器原理分析与基本应用
  4. sql 中 left join 的使用
  5. 【GoLang】GoLang 错误处理 -- 使用 error is value 的思路处理,检查并处理error
  6. 51nod 1211 数独
  7. Eclipse 和 NetBeans 快捷键即其他常用功能比较
  8. Android Studio常用快捷键汇总(mac)
  9. 利用Azure高级存储搭建高性能Linux服务器(2)
  10. hdu 3911 Black And White(线段树)
  11. FZU 2147 A-B Game(数学)
  12. 2017春 前端自动化(二) 页面自动刷新、sass与css转换的使用、pxToRem直观转换
  13. 使用Dism备份和全新恢复系统
  14. HashMap/HashSet,hashCode,哈希表
  15. 简单记录numpy库的某些基本功能
  16. ArcGIS JS API多线程克里金插值
  17. python编程之如何在Windows上安装python
  18. android -------- 混淆打包报错(warning - InnerClass annotations are missing corresponding EnclosingMember annotations)
  19. Java标准I/O流编程一览笔录
  20. 关于 wsdl2Java 自动生成客户端调取webservice接口

热门文章

  1. 基于二叉树和双向链表实现限制长度的最优Huffman编码
  2. sublime 支持 vue 语法
  3. VS中创建自定义一个VC工程为基础的开发向导的总结
  4. Kinect 开发 —— 常见手势识别(上)
  5. deep-in-es6(四)
  6. HDU 4696 Answers 水题
  7. BZOJ1814: Ural 1519 Formula 1(插头Dp)
  8. django模型系统二
  9. Android 关于expandableListView childrenView 点击改变颜色
  10. 74.sscanf数据扫描