2009-12-06 87 views
0

我看了很多帖子,我没有看到mystake。路线MVC问题

可以请一些身体帮助我。

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

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

回答

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

尝试删除:

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

记住更多的一般途径应该是在你的路由表的底部,而更为具体的路线应该来在上面。