zoukankan      html  css  js  c++  java
  • 主攻ASP.NET.3.5.MVC架构之重生: URL Routing (三)

    ASP.NET MVC路由匹配检测组件RouteDebug.dll

    URL Routing

    URL Routing是与Asp.Net3.5 MVC框架独立的一个功能。

    可以在传统ASP.Net应用中使用

    优化路由设置

    路由

                routes.MapRoute(

                    "products-route", // 路由名称

                    "products/{category}", // 带有参数的 URL

                    new { controller = "products", action = "category", }// 参数默认值

                );

    ProductsController.cs

            //

            //GET:/Products/Category

            public ActionResult Category()

            {

                return View();

            }

    Category.aspx

    <%=Html.RouteLink("Show Beverages","products-route",new{category="beverages"} )%>

     

    网上查的资料

    一个有意思的routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    IgnoreRoute是定义在ASP.NET MVC中,基于RouteCollection类型的扩展方法。它会向RouteCollection中添加一个Route对象,而这个Route对象在匹配成功时返回的RouteData对象,其RouteHandler属性便为一个StopRoutingHandler,于是余下的Routing规则也不会继续匹配了——这一点和RouteBase对象返回null不同,因为如果返回null,则余下的规则还会依次匹配。如果返回了一个包含StopRoutingHanderRouteData,则剩下的Routing规则全部跳过。ASP.NET Routing是一个通用的组件,它不涉及到任何具体的请求处理方式。如果您需要,也可以自己基于它进行开发

    代码

            public static void RegisterRoutes(RouteCollection routes)

            {

                routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

                routes.MapRoute(

                    "List", // 路由名称

                    "{controller}/list-{page}.html", // 带有参数的 URL

                    new { controller = "Service", action = "Index", page = UrlParameter.Optional }, // 参数默认值

                    new string[] { "AllFor.Controllers" }

                );

                routes.MapRoute(

                    "Category", // 路由名称

                    "{controller}/c-{id}-{page}", // 带有参数的 URL

                    new { controller = "Service", action = "Category", id = UrlParameter.Optional, page = UrlParameter.Optional }, // 参数默认值

                    new string[] { "AllFor.Controllers" }

                );

                routes.MapRoute(

                    "Detail", // 路由名称

                    "{controller}/d-{id}.html", // 带有参数的 URL

                    new { controller = "Service", action = "Detail", id = UrlParameter.Optional}, // 参数默认值

                    new string[] { "AllFor.Controllers" }

                );

                routes.MapRoute(

                    "ActionHtml", // 路由名称

                    "{controller}/{action}.html", // 带有参数的 URL

                    new { controller = "Default", action = "Index"}, // 参数默认值

                    new string[] { "AllFor.Controllers" }

                );

                routes.MapRoute(

                    "DefaultHtml", // 路由名称

                    "{controller}/{action}/{id}.html", // 带有参数的 URL

                    new { controller = "Default", action = "Index", id = UrlParameter.Optional }, // 参数默认值

                    new string[] { "AllFor.Controllers" }

                );

                routes.MapRoute(

                    "Default", // 路由名称

                    "{controller}/{action}/{id}", // 带有参数的 URL

                    new { controller = "Default", action = "Index", id = UrlParameter.Optional }, // 参数默认值

                    new string[] { "AllFor.Controllers" }

                );

            }

            protected void Application_Start()

            {

                 RegisterRoutes(RouteTable.Routes);

                //RouteDebug.RouteDebugger.RewriteRoutesForTesting(RouteTable.Routes);

            }

  • 相关阅读:
    日本处方药物
    oqtane 与 OrchardCore 两个快速Blazor框架
    Radzen 一个快速开发Net Core BLazor 框架,代替 Lightswitch,含 Free组件
    IBM罗睿兰CEO任期内给全球投资者的最后一封信(转)
    serenity 开发框架(Net Core 2.2 C#)
    商务成本一体化管理系统(施工企业)--> 个性定制标准化流程管理系统 ---- 土木众联
    一站式招采管理系统(施工企业)---- 土木众联
    资信证件数字化管理系统(施工企业)---- 土木众联公司
    API 测试工具 --- 图形化界面
    014_浅说 XSS和CSRF
  • 原文地址:https://www.cnblogs.com/cube/p/2524116.html
Copyright © 2011-2022 走看看