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

MVC中url路由规则的更多相关文章

  1. MVC的URL路由规则

    MVC的URL路由规则 Routing的作用:它首先是获取到View传过来的请求,并解析Url请求中Controller和Action以及数据,其次他将识别出来的数据传递给Controller的Act ...

  2. mvc中Url.RouteUrl或者Html.RouteLink实现灵活超链接,使href的值随路由名称或配置的改变而改变[bubuko.com]

    mvc,超链接除了直接写在a标签的href内还可以使用路由规则来生成,这样在改变了路由规则或者路由名称时不用再去代码中更改href的值,而且还容易遗漏.借助Url.RouteUrl或者Html.Rou ...

  3. asp.net mvc 通过修改路由规则来实现页面的URL多参数传递

    [原文]http://blog.csdn.net/risingsun001/article/details/9068187 修改MVC3中的路由规则 在Global.asax.cs中,修改路由规则 原 ...

  4. MVC中的路由

    authour: chenboyi updatetime: 2015-05-02 16:10:04 friendly link:   目录 1,思维导图 2,MVC处理机制简图(讲解路由解析) 3,默 ...

  5. MVC中URL传多个参数

    1.mvc中url传递多个参数不能直接使用&,会报错(从客户端(&)中检测到有潜在危险的 Request.Path 值) 方法①:使用?---/Home/Index/?id=xxx&a ...

  6. .NetCore MVC中的路由(1)路由配置基础

    .NetCore MVC中的路由(1)路由配置基础 0x00 路由在MVC中起到的作用 前段时间一直忙于别的事情,终于搞定了继续学习.NetCore.这次学习的主题是MVC中的路由.路由是所有MVC框 ...

  7. Django中url匹配规则的补充

    Django中url匹配规则是在urls.py文件中配置的. 1.关于正则匹配优先级 在url匹配列表中,如果第一条和第二条同时满足匹配规则,则优先匹配第一条. 在url匹配列表中,如果第一条为正则模 ...

  8. .NetCore MVC中的路由(2)在路由中使用约束

    p { margin-bottom: 0.25cm; direction: ltr; color: #000000; line-height: 120%; orphans: 2; widows: 2 ...

  9. phpcms url路由规则、多站点、PC手机切换

    解决一个分站点pc手机共存的问题 首先需要有PC手机两套模板.通过修改url路由规则,在同一目录下生成PC手机两套静态网站,PC使用默认url路由规则,手机端使用文件名追加“_m”的路由规则. 然后通 ...

随机推荐

  1. jquery08

    <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...

  2. LinkedHashMap<String, Bitmap>(0, 0.75f, true) LinkedHashMap的加载因子和初始容量分配

    今天上午在CSDN的论坛里看到有朋友提的问题如下: /** @param maxSize Maximum sum of the sizes of the Bitmaps in this cache * ...

  3. amaze ui表格斑马纹效果

    amaze ui表格斑马纹效果 需要注意的是样式的写法,都是 am-table- ,很好记的 如果是条纹就是striped,如果是hover状态就是hover 用法很简单,点对应class,不同的cl ...

  4. c++中sizeof()的用法介绍

    1.      定义 sizeof是一个操作符(operator). 其作用是返回一个对象或类型所占的内存字节数. 2.      语法 sizeof有三种语法形式: 1)  sizeof (obje ...

  5. vuejs实现表格分页

    http://www.cnblogs.com/landeanfen/p/6054654.html#_label3_8 <html xmlns="http://www.w3.org/19 ...

  6. HDU 4696 Answers 水题

    http://acm.hdu.edu.cn/showproblem.php?pid=4696 由题意可知 1<=Ci<=2 而且图上一定有环 那么我们可以得出: 只要存在奇环(即Ci=1) ...

  7. Day 3 EX 购物车自写

    # -*- coding: utf_8 _*_# Author:Vi import copygoods = [0,[1,'iphone',20],[2,'ipad',2500]]salary = in ...

  8. 今日SGU 5.1

    SGU 100 题意: 普通的a+b #include<bits/stdc++.h> #define de(x) cout<<#x<<"="&l ...

  9. 获取图书信息api

    https://www.zhihu.com/question/20306982 http://code.juhe.cn/docs/1109 https://developers.douban.com/ ...

  10. HDU 1533 Going Home(KM完美匹配)

    HDU 1533 Going Home 题目链接 题意:就是一个H要相应一个m,使得总曼哈顿距离最小 思路:KM完美匹配,因为是要最小.所以边权建负数来处理就可以 代码: #include <c ...