无法在我的ASP.NET MVC应用程序中正确配置路由

时间:2016-08-22 07:15:58

标签: c# asp.net asp.net-mvc routes asp.net-mvc-routing

我在asp.net mvc app中配置我的路由,其中​​有多条路由,只有第一条和最后一条路由可以工作,但是我的route.config文件中心定义的路由不起作用。以下是代码:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    routes.MapRoute(
        name: "Doctor",
        url: "admin/doctor/{action}/{id}",
        defaults: new { controller = "admin", action = "AddDoctor", id = UrlParameter.Optional }
        );
    routes.MapRoute(
        name: "Admin",
        url: "admin/{action}/{id}",
        defaults: new { controller = "admin", action = "Login", id = UrlParameter.Optional }
        );

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

}

1 个答案:

答案 0 :(得分:0)

您似乎忘记了controller路线的Admin部分。

您应该将其定义如下:

routes.MapRoute(
    name: "Admin",
    url: "admin/{controller}/{action}/{id}",
    defaults: new { controller = "admin", action = "Login", id = UrlParameter.Optional }
    );