在.NET MVC中为1个控制器定义多个静态路由和1个动态路由

时间:2017-08-30 13:17:34

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

我已经定义了几条静态路由,如下所示:

   routes.MapRoute(
            name: "LogOutRoute",
            url: "Index/LogOut",
            defaults: new { controller = "Index", action = "LogOut" }
             );
   routes.MapRoute(
            name: "Tutorials",
            url: "Index/Tutorials",
            defaults: new { controller = "Index", action = "Tutorials" }
            );

第三个是动态路线,如下所示:

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

我想为我的索引控制器定义这两个静态路由:

/Index/Tutorials
/Index/LogOut

其他每条路线都应指向:

/Index/{id}

我现在定义它的方式适用于2个静态路由,但是当我尝试传递这样的参数时,这不是以下两个静态路由之一:

http://localhost:60617/Index/12345/

如果12345是ID,我会收到以下错误:

The resource cannot be found.

如何正确定义这些路线?有人可以帮助我吗?

这是路线类:

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


        routes.MapRoute(
        name: "LogOutRoute",
        url: "Index/LogOut",
        defaults: new { controller = "Index", action = "LogOut" }
         );
        routes.MapRoute(
        name: "Tutorials",
        url: "Index/Tutorials",
        defaults: new { controller = "Index", action = "Tutorials" }
        );

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


    }
}

1 个答案:

答案 0 :(得分:1)

映射路线的顺序非常重要。

应在更具体的路由之后映射通​​用路由,以避免路由冲突。

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

        routes.MapRoute(
            name: "LogOutRoute",
            url: "Index/LogOut",
            defaults: new { controller = "Index", action = "LogOut" }
        );

        routes.MapRoute(
            name: "Tutorials",
            url: "Index/Tutorials",
            defaults: new { controller = "Index", action = "Tutorials" }
        );

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

        routes.MapRoute(
          name: "ResetPwdRoute",
          url: "User/ResetPwd/{id}",
          defaults: new { controller = "User", action = "ResetPwd", id = UrlParameter.Optional }
        );

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