zoukankan      html  css  js  c++  java
  • URL路由

     在ASP.NET中

    重写URL:

      创建一个web项目,内容如下:

        public class RouteConfig
        {
            public static void RegisterRoutes(RouteCollection routes) {
                routes.MapPageRoute("default","","~/Default.aspx");
                routes.MapPageRoute("cart1","cart","~/Store/Cart.aspx");
                routes.MapPageRoute("cart2","apps/shopping/finish","~/Store/Cart.aspx");
            }
        }

    MapPageRoute方法有多个重载,最常用的就是给定3个参数。

    参数1:路由的名称。

    参数2:应用程序支持的新的虚拟路径。(http://localhost:60923/apps/shopping/finish 和 http://localhost:60923/cart 都会指向Cart.aspx这个页面)

    参数3:新的虚拟路径所指向的窗体

    下面是Global.asax文件中的代码
    protected void Application_Start(object sender, EventArgs e)
            {
                RouteConfig.RegisterRoutes(RouteTable.Routes);
            }

    ASP.NET MVC中

    当mvc程序启动的时候会主动调用Global.asax文件中的Application_Start方法,在该方法中又会主动调用静态方法RouteConfig.RegisterRoutes,这个时候会将URL和路由规则逐一进行匹配。

    //使用routes.MapRoute 方法注册路由
    routes.MapRoute("MyRoute", "{controller}/{action}");
    
    //创建默认的路由规则
    routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
    
    //使用静态变量的 Url片段(匹配:http://localhost:20234/xxx/Home/Index)
    routes.MapRoute("", "xxx/{controller}/{action}", new { controller = "Home", action = "Index" });
    
    //使用静态片段和默认值为特定的路由创建别名
    routes.MapRoute("ShopSchema", "shop/{action}", new { controller = "Home"});//这里用户在url中输入shop,然后会被替换成Home。
    routes.MapRoute("ShopSchema", "oldController/oldAction", new { controller = "Home",action="Index"});
    
    //在控制器的方法中,接收Url中的参数
    public ActionReulst CustomVariable(){
    ViewBag.variable=RouteData.Values["id"];//获取Url中,id片段的值
    return View();
    }
    public static void RegisterRoutes(RouteCollection routes)
            {
                routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    
                //当MVC框架解析浏览器中的URL时,倘若检测到了controller片段为Home,则会优先使用该条路由进行匹配。
                routes.MapRoute(
                    "AddControllerRoute", "Home/{action}/{id}/{*catchAll}", new { controller = "Home", action = "Index", id = UrlParameter.Optional }, new[] { "URLsAndRoutes.AddtionalControllers" });
           //定义一个可变长路由。以*开头,表示支持多个片段数。
                routes.MapRoute("MyRoute", "{controller}/{action}/{id}/{*catchAll}", new { controller = "Home", action = "Index", id = UrlParameter.Optional });
    }
    //约束路由
    //1、使用正则表达式约束路由,和使用指定的值来约束路由(controller以H打头(正则表达式),action只能是Index或者是About(指定的值))
    routes.MapRoute("ShopSchema", "{controller}/{action}/{id}/{*catchall}", new { controller = "Home",action="Index",id=UrlParameter.Optional},
    new {controller="^H.*",action="^Index$|^About$"},
    new []{"URLsAndRoutes.Controllers"}
    );
    //2使用HTTP方法约束路由。(好像用的不是太多)controller以H打头(正则表达式),action只能是Index或者是About(指定的值))
    routes.MapRoute("ShopSchema", "{controller}/{action}/{id}/{*catchall}", new { controller = "Home",action="Index",id=UrlParameter.Optional},
    new {controller="^H.*",action="Index|About",httpMethod=new HttpMethodConstraint("GET")},
    new []{"URLsAndRoutes.Controllers"}
    );
    
    还有 使用类型约束, 
       值类型约束
       自定义约束。
  • 相关阅读:
    Hbase 性能改进
    HBase总结(十一)hbase Java API 介绍及使用示例
    Java中如何遍历Map对象的4种方法
    Jsp分页实例---假分页
    Jsp分页实例---真分页
    Java正则表达式
    平均时间复杂度为O(nlogn)的排序算法
    常见排序算法--简单排序
    [kuangbin带你飞]专题一 简单搜索
    [kuangbin带你飞]专题一 简单搜索
  • 原文地址:https://www.cnblogs.com/vichin/p/9898745.html
Copyright © 2011-2022 走看看