• MVC路由测试


    先注册路由
                routes.MapRoute(
                    name: "Member",
                    url: "Member/{action}/{page}",
                    defaults: new { controller = "MemberCenter", action = "List", id = UrlParameter.Optional },
                    constraints: new { action = "index|list|detail", page = "d+" }
                );
                routes.MapRoute(
                    name: "Default",
                    url: "{controller}/{action}/{id}",
                    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
                );
    注意路由约束是完全匹配的,比如:page = "d+",实际执行时会转换为: page = "^d+$"来匹配。

    到视图文件中加入以下内容测试:
    <p>
        @RouteTable.Routes.GetVirtualPath(
            Request.RequestContext,
                new RouteValueDictionary(new
                {
                    controller = "MemberCenter",
                    action = "detail"
                })
        ).VirtualPath;
    </p>

    这里只能匹配第二个(第一个要求page参数):/MemberCenter/detail;

    这次加入一个带page参数的试试:

            RouteTable.Routes.GetVirtualPath(
                Request.RequestContext,
                    new RouteValueDictionary(new
                    {
                        controller = "MemberCenter",
                        action = "Detail",
                        page = "TEST"
                    })
            ).VirtualPath

    /MemberCenter/Detail?page=TEST

    需要特别注意的是如果把第二注册的参数改为page = UrlParameter.Optional会导致路由解析重叠覆盖,解析结果:

    /MemberCenter/detail; /MemberCenter/Detail/TEST 都是后注册的第二条

    如果加一个链接: @Html.ActionLink("测试链接", "detail", "MemberCenter", new { page = "testpage" }, null);  始终指向 http://localhost:44622/MemberCenter/detail/testpage

     

  • 相关阅读:
    网络协议
    面向对象三大特性之多态
    面向对象三大特性之封装
    面向对象三大特性之继承
    python面向对象编程
    常用模块之configpaser与shutil
    XML模块
    Python模块之OS,subprocess
    Python日志(logging)模块,shelve,sys模块
    Linux 配置 Nginx
  • 原文地址:https://www.cnblogs.com/shi5588/p/4173204.html
Copyright © 2020-2023  润新知