使用 HTML.ActionLink 的 ASP.net MVC site.master 链接
我的 site.master
中有以下菜单代码:
<ul id="menu">
<li><%= Html.ActionLink("My Contact Info", "DetailsbyUserName/" + Html.Encode(Page.User.Identity.Name), "Users")%></li>
</ul>
当我将鼠标悬停在 URL 上时,我看到它指向:
http://site/Users/DetailbyUserName/[name]
这是正确的。
问题是,当我在下面的 Users
控制器类中放置断点时:
public ActionResult DetailsbyUserName(string loginName)
{
UserInfo user = repo.GetUserByName(loginName);
return View(user);
}
似乎 loginName
参数始终为 null
。
有什么建议么?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是您没有设置路由来指定名为“loginName”的参数。
我猜测您的默认路由正在吞噬您的请求,并尝试将 [name] 值分配给名为“id”的参数。 如果您将参数名称从“loginName”更改为“id”,我敢打赌它会对您有用。
请记住,路由引擎将每个 URL 段映射到一个命名参数。 默认路由如下所示:
“{controller}/{action}/{id}”
。 如果您想要一个名为“loginName”的参数,则必须提供一个包含段"{controller}/{action}/{loginName}"
的 Route,这与默认路由,这样默认路由就不会先匹配到它。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.尝试:
如果您的控制器是这样的:
我想您必须在 ActionLink 的 RouteValues 参数中使用 new {veryVeryLongParameterName = "YourParamValue" } 。
而且,您还需要一条路线。
我对此也很陌生,至少这是我对 ActionLinks 的理解,希望有人能更好地解释它。
try:
if your controller is something like this:
I guess you have to use new { veryVeryLongParameterName = "YourParamValue" } in the ActionLink's routeValues parameter.
And also, you need a route for that.
I'm very new to this too, at least that's what I understood about ActionLinks, hope someone can explain it better.