zoukankan      html  css  js  c++  java
  • 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路由了。

  • 相关阅读:
    Python_Tips[3] -> sort/sorted 排序函数
    Python_Tips[2] -> 函数延后估值及字节码分析
    Python_Tips[1] -> 利用 Python 的字典实现 Switch 功能
    Python_Tips[0] -> 关于 import
    Python与数据库[2] -> 关系对象映射/ORM[4] -> sqlalchemy 的显式 ORM 访问方式
    Python与数据库[2] -> 关系对象映射/ORM[3] -> sqlalchemy 的声明层 ORM 访问方式
    Python与数据库[2] -> 关系对象映射/ORM[2] -> 建立声明层表对象的两种方式
    JS实现网页选取截屏 保存+打印 功能(转)
    nodejs+koa2 实现一个get请求
    windwos下安装使用nginx(转)
  • 原文地址:https://www.cnblogs.com/xishuqingchun/p/4218977.html
Copyright © 2011-2022 走看看