I have the following code in my site.master for a menu:
<ul id="menu">
<li><%= Html.ActionLink("My Contact Info", "DetailsbyUserName/" + Html.Encode(Page.User.Identity.Name), "Users")%></li>
</ul>
When I hover over the URL I see that it points to:
http://site/Users/DetailbyUserName/[name]
which is correct.
The issue is that when I put a breakpoint in the Users controller class below:
public ActionResult DetailsbyUserName(string loginName)
{
UserInfo user = repo.GetUserByName(loginName);
return View(user);
}
it seems that the loginName parameter is always null.
Any suggestions?
The problem is that you don’t have a Route set up to specify a parameter called “loginName”.
I’m guessing that your default Route is swallowing your request, and trying to assign the [name] value to a parameter called “id”. If you change the name of the parameter to “id” from “loginName”, I bet it will work for you.
Remember that the routing engine maps each URL segment to a named parameter. The default route looks like:
"{controller}/{action}/{id}". If you want to have a parameter named “loginName”, you would have to come up with a Route that had the segments"{controller}/{action}/{loginName}", that was different from the default route, so that the default route didn’t match it first.