使用 HTML.ActionLink 的 ASP.net MVC site.master 链接

发布于 2024-07-25 20:55:21 字数 649 浏览 9 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萌酱 2024-08-01 20:55:21

问题是您没有设置路由来指定名为“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.

情感失落者 2024-08-01 20:55:21

尝试:

Html.ActionLink("My Contact Info", "DetailsbyUserName", "Users", new { loginName = Html.Encode(Page.User.Identity.Name), null);

如果您的控制器是这样的:

public ActionResult DetailsbyUserName(string veryVeryLongParameterName);

我想您必须在 ActionLink 的 RouteValues 参数中使用 new {veryVeryLongParameterName = "YourParamValue" } 。

而且,您还需要一条路线。

我对此也很陌生,至少这是我对 ActionLinks 的理解,希望有人能更好地解释它。

try:

Html.ActionLink("My Contact Info", "DetailsbyUserName", "Users", new { loginName = Html.Encode(Page.User.Identity.Name), null);

if your controller is something like this:

public ActionResult DetailsbyUserName(string veryVeryLongParameterName);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文