代码之家  ›  专栏  ›  技术社区  ›  Cédric Boivin

路由MVC问题

  •  0
  • Cédric Boivin  · 技术社区  · 15 年前

    我读了很多文章,我看不到我的文章。

    有人能帮我吗?

    有我的global.asax

    public static void RegisterRoutes(RouteCollection routes)
            {
                routes.IgnoreRoute("{resource}.axd/{*pathInfo}");                
               routes.MapRoute(
                    "Default",                                              // Route name
                    "{controller}/{action}/{id}",                           // URL with parameters
                    new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
                );
               routes.MapRoute("UserName", "Account/{action}/{username}",
              new { action = "EditProfile", controller = "Account" });   
    
            }
    
            protected void Application_Start()
            {
                RegisterRoutes(RouteTable.Routes);
            }
    

    当我使用

    <%= Html.ActionLink("Edit Account", "EditProfile", "Account", new { username = Html.Encode(Page.User.Identity.Name) },null) %>
    

    我得到这个URL

    http://localhost:8458/Account/EditProfile?username=cboivin

    如果我尝试直接调用URL http://localhost:8458/Account/EditProfile/cboivin

    不工作…

    我的会计主任有我的方法

     public ActionResult EditProfile(string username)
            {
    
                return View(ServiceFactory.UserAccount.GetUserByUserName(new GetUserByUserNameArgs(username)));
            }
    

    我不知道我的错误在哪里。有人能帮我吗?谢谢。

    2 回复  |  直到 15 年前
        1
  •  6
  •   Paul Creasey    15 年前

    路线是自上而下的,你的“一网打尽”一般路线需要最后一条。

    routes.MapRoute("UserName", "Account/{action}/{username}",
              new { action = "EditProfile", controller = "Account" });
    
    
    routes.MapRoute(
                "Default",                                              // Route name
                "{controller}/{action}/{id}",                           // URL with parameters
                new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
            );
    
        2
  •  0
  •   Orson    15 年前

    尝试删除:

    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    

    请记住,更一般的路由应该在路由表的底部,而更具体的路由应该在顶部。