2010-11-29 64 views
0

我有这个在我的Global.asax.cs不是渲染:@ Html.ActionLink按预期

routes.MapRoute(
      "User", 
      "User/{username}/{action}", 
      new { controller = "User", action = "Index", username = "*" } 
     ); 

然后在我的_Layout.cshtml我有这样的代码:

  <ul id="menu"> 
      @if (!String.IsNullOrEmpty(Context.User.Identity.Name)) 
      { 
       <li>@Html.ActionLink("Home", "Home", new { controller = "User" }, new { username = Context.User.Identity.Name })</li> 
      } 


      </ul> 

     </div> 
    </div> 

的东西是,它会在第一次在这里摆动时正确显示链接。 (链接将会是/ User/rob/Home,其中“rob”是一个用户名,如果我在页面的其他地方导航,然后点击我的链接,链接将显示为/ User/*/Home。码,Context.User.Identity.Name每次是正确的。

我失去了真正的基本的东西吗?我不知道该怎么寻找。

+0

尝试将此添加到页面<%= Context.User.Identity.Name%>并查看会发生什么。 – Iain 2010-11-29 02:22:44

+0

我不确定你正在使用什么ActionLink重载,这里是我认为应该是的:Html.ActionLink(“Home”,“Home”,“User”,new {username = Context.User.Identity.Name} ,新{}) – Iain 2010-11-29 02:28:20

回答

0

这就是你应该期望给出正是这条路线。您不会在路由值字典中指定username,而是在HTML属性中指定username,所以它将采用路由*中的默认值。您应该使用允许您将控制器和操作指定为具有其他路由的字符串的签名值在字典中。

@if (!String.IsNullOrEmpty(Context.User.Identity.Name)) 
{ 
    <li>@Html.ActionLink("Home", "Home", "User" new { username = Context.User.Identity.Name }, null)</li> 
}