Registering Multiple Routes in MVC asp.net

南楼画角 提交于 2020-01-02 11:27:17

问题



I want to do make several map route in MVC that both like be same.

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

id=abcd controller=home action=index

I used bellow code but it doesn't work

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

    }

回答1:


It will not work because both routes have the same format.

So the MVC Routing Engine cannot differentiate between both the url patterns.

Try writing the Controller directly into the url pattern.

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

    }


来源:https://stackoverflow.com/questions/16139743/registering-multiple-routes-in-mvc-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!