在MVC asp.net中注册多个路由

问题描述 投票:2回答:1

我想在MVC中映射多个具有不同顺序参数的路由:

localhost:1010/abcd/home/index
localhost:1010/home/index/abcd

id=abcd
controller=home
action=index

我尝试了下面的代码,但是不起作用。

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        "ShoppingManagment",
        "{id}/{controller}/{action}",
        new { controller = "ShoppingManagment",
            action = "ShoppingManagment", id = UrlParameter.Optional });


    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home",
            action = "Index", id = UrlParameter.Optional }
    );
}
asp.net-mvc asp.net-mvc-routing maproute
1个回答
12
投票

因为两个路由具有相同的格式,所以将不起作用。

因此,MVC路由引擎无法区分这两种URL模式。

尝试将Controller直接写入url模式。

public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
          "ShoppingManagment",
          "{id}/ShoppingManagment/{action}",
          new { controller="ShoppingManagment", action = "ShoppingManagment", id = UrlParameter.Optional });


        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home",
             action = "Index", id = UrlParameter.Optional }
        );

    }

推荐问答