asp.net mvc3如何自定义路由

asp.net mvc3如何自定义路由,第1张

在Global.asax文件里面添加自己的路由

参考代码

using System.Web

using System.Web.Mvc

using System.Web.Routing

using PersonalWebSite.UI.Common

namespace PersonalWebSite.UI

{

    public class MvcApplication : System.Web.HttpApplication

    {

        public static void RegisterGlobalFilters(GlobalFilterCollection filters)

        {

            filters.Add(new HandleErrorAttribute())

        }

        public static void RegisterRoutes(RouteCollection routes)

        {

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

            routes.MapRoute(

                "ArticleClassification", // 导航路由(带参数)

                "article_{classificationId}_{pageCount}_{pageIndex}.html", // 带有参数的 URL

                new { controller = "Article", action = "Index", classificationId = "classificationId", pageCount = "pageCount", pageIndex = "pageIndex" } // 参数默认值

            )

            routes.MapRoute(

                "Navigation_Two_Parameter", // 导航路由(带参数)

                "{controller}_{pageCount}_{pageIndex}.html", // 带有参数的 URL

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

            )

            routes.MapRoute(

                "Navigation_One_Parameter", // 导航路由(带参数)

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

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

            )

            routes.MapRoute(

                "Navigation", // 导航路由

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

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

            )

            routes.MapRoute(

                "Admin", // 后台管理路由

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

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

            )

            routes.MapRoute(

                "Default", // 默认路由

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

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

            )

        }

        protected void Application_Start()

        {

            AreaRegistration.RegisterAllAreas()

            RegisterGlobalFilters(GlobalFilters.Filters)

            RegisterRoutes(RouteTable.Routes)

            RegisterView()//注册自定义视图访问规则

        }

        protected void RegisterView()

        {

            ViewEngines.Engines.Clear()

            ViewEngines.Engines.Add(new MyViewEngine())

        }

    }

}

方法步骤

1、找到路由配置文件

2、打开文件之后,添加代码:

3、代码:routes.MapRoute(

"product-details",

"product/{id}",

new

{

controller

=

"Product",

action

=

"Details",

id

=

@"d"}

)

说明:

(1)MapRoute方法的第一个参数是本条路由的名称,如果指定,则每条路由名称应唯一,也可以放空不填

(2)第二个参数指的是url的格式,如product/123,表示是的编号为123的产品的详细信息,还可以再加复杂点,如/product/{category}/{id},表示"产品/类别/编号"

(3)第三个参数设置默认值及限制,可以使用正则表达式

(4)其实还有第四个参数,可以指定不同的area

(5)路由可以配置多条,并且按顺序执行,在前面的优先级较高

配置好,编译运行,可以直接访问:/product/123,而不是原来的/product/details/123

4、还可以配置带有分类参数的路由,使得访问路径可以变成:/product/food或/product/food/2,前者表示food分类下的所有商品,后者表示food分类下编号为2的商品

5、还可以对分类名进行限制,只允许出现其中的名称,示例表示只允许出现四种可能,其他的分类名都是非法的

6、如果你创建了Area,那么你需要添加一个命名空间,不然会产生冲突,这个是主要路由配置示例

7、这个是Area中的路由配置示例,其中,每个area都会有一个默认的配置文件,你打开之后,就在这里设置命名空间


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/bake/11845263.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-19
下一篇 2023-05-19

发表评论

登录后才能评论

评论列表(0条)

保存