MVC 5.0 RegisterRoutes不起作用

时间:2014-10-05 21:43:11

标签: asp.net asp.net-mvc asp.net-mvc-5

任何人都知道为什么这不起作用,我用谷歌搜索它但似乎没有任何效果。我想要做的就是允许www.abcdefg.com/username映射到个人资料页面,但它不起作用,我无法找到原因。

public static void RegisterRoutes(RouteCollection routes)
        {
         routes.MapRoute(
        "Default",                                              // Route name
        "{controller}/{action}/{id}",                           // URL with parameters
        new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
        , new { controller = @"(admin|help|profile|Settings)" } // Constraints
    );
                routes.MapRoute(
                  "Users",
                  "{username}",
                  new { controller = "Home", action = "Index", username = "" });
            }

1 个答案:

答案 0 :(得分:1)

您必须将默认路线添加为最后路线

public static void RegisterRoutes(RouteCollection routes)
        {
         routes.MapRoute(
           "Users",
           "{username}",
           new { controller = "Home", action = "Index", username = "" });

         routes.MapRoute(
           "Default",                                              // Route name
           "{controller}/{action}/{id}",                           // URL with parameters
           new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
           , new { controller = @"(admin|help|profile|Settings)" } // Constraints
    );