@Html.ActionLink not Rendering as Expected
I have this in my Global.asax.cs:
routes.MapRoute(
开发者_开发百科 "User",
"User/{username}/{action}",
new { controller = "User", action = "Index", username = "*" }
);
Then on my _Layout.cshtml I have this code:
<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>
The thing is, it will render the link properly the first time it swings through here. (Link will be /User/rob/Home where "rob" is a username. If I navigate elsewhere on the page and then click back on my link, the link is rendered as /User/*/Home. When I step through the code, Context.User.Identity.Name is correct every time.
Am I missing something really basic here? I'm not sure what to search for.
That's exactly what you should expect given that route. You don't specify username
in the route values dictionary but in the HTML attributes, so it takes the default from the route, *
. You should be using the signature that allows you to specify both the controller and the action as strings with additional route values in the dictionary.
@if (!String.IsNullOrEmpty(Context.User.Identity.Name))
{
<li>@Html.ActionLink("Home", "Home", "User" new { username = Context.User.Identity.Name }, null )</li>
}
精彩评论