MVC的URL路由规则

Routing的作用:它首先是获取到View传过来的请求,并解析Url请求中Controller和Action以及数据,其次他将识别出来的数据传递给Controller的Action(Controller的方法)。这是Routing组件的两个重要的作用!

下面我们从几个例子来讲解一下Url路由的使用。

MapRoute()有6个方法可以重载,下面举5个例子相应介绍!

实例一:首先讲解的是系统默认提供的路由格式,下面是系统给的默认代码:

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // 路由名称
                "{controller}/{action}/{id}", // 带有参数的 URL
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // 参数默认值
            );
          }

Url格式为:http://localhost:0000/home/index  对应规则为:{controller}/{action}/{id}  黑体部分就是对应部分。这还是有默认值的情况。

详细匹配应该为:http://localhost:0000/Custom/Detials/1   去掉主机域名,剩下的对应就是匹配Controller和actiong了。通过Routing组件解析这个Url,Controller是Custom,Action是Detials。传递过去的Id是1。

这就是使用了MapRoute( string name, string url, object defaults);这个方法的重载。

实例二:不使用默认值的Url路由规则

  函数头:MapRoute( string name, string url);

   routes.MapRoute("没有默认值路由规则", "{controller}/{id}-{action}");

  适合的Url例子:http://localhost:0000/Custom/1-Detials  

  它将不匹配http://localhost:0000/

实例三:带名称空间的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[] { "MvcDemo.Controllers" }//命名空间
          );

Url:http://localhost:0000/Custom/1-Detials

这个例子是带命名空间的路由规则,这在Aeras使用时非常有用。不多说,后面再说!

实例四:带约束的路由规则

      函数头: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

实例五:带名称空间,带约束,带默认值的路由规则

函数头: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

实例六:捕获所有的路由

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路由规则

    Routing:首先获取视图页面传过来的请求,并接受url路径中的controller和action以及参数数据,根据规则将识别出来的数据传递给某controller中的某个action方法 MapR ...

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

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

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

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

  4. ASP.NET MVC 的URL路由介绍

    在这个教程中,向你介绍每个ASP.NET MVC一个重要的特点叫做URL路由.URL路由模块是负责映射从浏览器请求到特定的控制器动作. 在教程的第一部分,你将学习标准路由表如何映射到控制器的动作.在教 ...

  5. MVC之URL路由

    注册路由规则集合 一个 Web 应用具有一个全局的路由表,该路由表通过 System. Web.Routing.RouteTable的静态只读属性 Routes 表示,该属性返回一个类型为 Syste ...

  6. 第二十一节:Asp.Net Core MVC和WebApi路由规则的总结和对比

    一. Core Mvc 1.传统路由 Core MVC中,默认会在 Startup类→Configure方法→UseMvc方法中,会有默认路由:routes.MapRoute("defaul ...

  7. URL路由规则实例

    1.设置支持路由和写路由规则

  8. 【基础】MVC路由规则

    一.RouteData解析过程 在ASP.NET MVC中,服务器收到来自客户端的请求后,会经过一些列的处理拿到请求的数据,比如在Pipeline 管线事件中,通过订阅适当的事件,将HttpConte ...

  9. vs2017 mvc 自定义路由规则 出现 404.0 错误代码 0x80070002

    自定义: WebApiConfig  里面最后增加 config.Services.Replace(typeof(IHttpControllerSelector), new NamespaceHttp ...

随机推荐

  1. 边工作边刷题:70天一遍leetcode: day 86-1

    Find Median from Data Stream 要点: 基本框架:两个heap:large,small把所有数二分.一个新的element.目标:维持heap中的元素个数相同.错误理解:新元 ...

  2. ifrog-1028 Bob and Alice are playing numbers(trie树)

    题目链接: Bob and Alice are playing numbers DESCRIPTION Bob and his girl friend are playing game togethe ...

  3. Hanoi塔

    2016-03-19 17:01:35 问题描述: 假设有三个命名为 A B C 的塔座 ,在塔座A上插有n个直径大小不相同,由小到大编号为1 ,2 ,3 ,··· ,n的圆盘,要求将A座上的圆盘移至 ...

  4. Loadrunner:场景运行较长时间后报错:Message id [-17999] was not saved - Auto Log cache is too small to contain the message.

    loadrunner运行时间较长后,跑数据过程老是失败,有如下error: Message id [-17999] was not saved - Auto Log cache is too smal ...

  5. Eclipse 分屏显示同一个文件

    场景 : 某个类很大,可能有数千行.当你想要将类开头部分与中间或者靠后的部分进行对比时,请follow如下步骤: Window -> Editor -> Toggle Split Edit ...

  6. Android service ( 一 ) 三种开启服务方法

    一. Service简介 Service是android 系统中的四大组件之一(Activity.Service.BroadcastReceiver.ContentProvider),它跟 Activ ...

  7. linux如何挂载windows下的共享文件

    说明:windows下有一共享文件夹APP,windows本地ip是192.168.9.155现在需要在linux服务器上挂载这个APP文件夹,linux服务器ip是192.168.9.200 操作记 ...

  8. 【C#】【Thread】Monitor和Lock

    所谓锁,就是之锁定的区域只能单个线程进入进行操作,其他线程在锁的外围等待.Monitor锁通过Monitor.Enter(obj)和Monitor.Exit(obj)来锁定和解锁.Lock锁则直接Lo ...

  9. 【转】【C#】序列化(Serialize)、反序列化(Deserialize)

    序列化又称串行化,是.NET运行时环境用来支持用户定义类型的流化的机制.其目的是以某种存储形成使自定义对象持久化,或者将这种对象从一个地方传输到另一个地方. .NET框架提供了两种串行化的方式: 1. ...

  10. 神奇的GO语言:空接口(interface)

    对于go语言来说,设计最精妙的应该是interface了,直白点说interface是一组method的组合.至于更加详细的描述,本文不做介绍,今天谈谈空接口. 空interface(interfac ...