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);

            }

  • 相关阅读:
    003 01 Android 零基础入门 01 Java基础语法 01 Java初识 03 Java程序的执行流程
    002 01 Android 零基础入门 01 Java基础语法 01 Java初识 02 Java简介
    001 01 Android 零基础入门 01 Java基础语法 01 Java初识 01 导学
    001 Android Studio 首次编译执行项目过程中遇到的几个常见问题
    Dora.Interception,为.NET Core度身打造的AOP框架 [2]:以约定的方式定义拦截器
    Dora.Interception,为.NET Core度身打造的AOP框架 [1]:更加简练的编程体验
    监视EntityFramework中的sql流转你需要知道的三种方式Log,SqlServerProfile, EFProfile
    轻量级ORM框架——第二篇:Dapper中的一些复杂操作和inner join应该注意的坑
    轻量级ORM框架——第一篇:Dapper快速学习
    CF888G Xor-MST(异或生成树模板)
  • 原文地址:https://www.cnblogs.com/cube/p/2524116.html
Copyright © 2011-2022 走看看