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

        }

原文地址:https://www.cnblogs.com/cube/p/2524116.html