路由MVC问题

时间:2009-12-06 15:46:03

标签: c# asp.net-mvc asp.net-mvc-2

我读了很多帖子,我没看到我的故事。

请一些人帮我。

有我的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) %>

我有这个网址

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

如果我尝试直接调用url就好了 http://localhost:8458/Account/EditProfile/cboivin

无法工作......

我的AccountController中有我的方法

 public ActionResult EditProfile(string username)
        {

            return View(ServiceFactory.UserAccount.GetUserByUserName(new GetUserByUserNameArgs(username)));
        }

我不知道我的错误在哪里。有些身体可以帮助我吗?感谢。

2 个答案:

答案 0 :(得分:6)

自上而下读取路线,您的“全部捕捉”一般路线需要持久。

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

答案 1 :(得分:0)

尝试删除:

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

请记住,更多的一般路线应位于路线表的底部,而更具体的路线应位于最顶层。

相关问题