asp.net mvc Html.ActionLink() 保留我不想要的路由值

发布于 2024-07-17 12:46:05 字数 812 浏览 6 评论 0原文

我的视图中有以下 ActionLink

<%= Html.ActionLink("LinkText", "Action", "Controller"); %>

,它创建以下 URL http://mywebsite.com/Controller/Action

假设我在末尾添加一个 ID,如下所示: http://mywebsite.com/Controller/Action/ 53 并导航到该页面。 在此页面上,我有上面指定的标记。 现在,当我查看它创建的 URL 时,它看起来像这样:

http://mywebsite.com/Controller/Action /53 (注意添加了 ID)

但我希望它删除 ID 并且看起来像原来一样,就像这样 http://mywebsite.com/Controller/Action(注意这里没有 ID)

有什么想法可以解决这个问题吗? 我不想使用硬编码的 URL,因为我的控制器/操作可能会改变。

I have the following ActionLink in my view

<%= Html.ActionLink("LinkText", "Action", "Controller"); %>

and it creates the following URL http://mywebsite.com/Controller/Action

Say I add an ID at the end like so: http://mywebsite.com/Controller/Action/53 and navigate to the page. On this page I have the markup I specified above. Now when I look at the URL it creates it looks like this:

http://mywebsite.com/Controller/Action/53 (notice the addition of the ID)

But I want it to remove the ID and look like it did originally, like this http://mywebsite.com/Controller/Action (notice no ID here)

Any ideas how I can fix this? I don't want to use hard coded URLs since my controller/actions may change.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(9

梦巷 2024-07-24 12:46:06

解决办法是指定我自己的路由值(下面第四个参数)

<%= Html.ActionLink("LinkText", "Action", "Controller", 
    new { id=string.Empty }, null) %>

The solution is to specify my own route values (the fourth parameter below)

<%= Html.ActionLink("LinkText", "Action", "Controller", 
    new { id=string.Empty }, null) %>
旧时光的容颜 2024-07-24 12:46:06

听起来您需要注册第二个“仅操作”路由并使用 Html.RouteLink()。 首先在应用程序启动时注册这样的路由:

routes.MapRoute("ActionOnly", "{controller}/{action}", 
   new { controller = "Home", action = "Index" } );

然后使用以下命令代替 ActionLink 来创建这些链接:

Html.RouteLink("About","ActionOnly")

It sounds like you need to register a second "Action Only" route and use Html.RouteLink(). First register a route like this in you application start up:

routes.MapRoute("ActionOnly", "{controller}/{action}", 
   new { controller = "Home", action = "Index" } );

Then instead of ActionLink to create those links use:

Html.RouteLink("About","ActionOnly")
时光磨忆 2024-07-24 12:46:06

问题是内置方法从您当前所在的 URL 以及您提供的内容获取输入。 您可以尝试以下操作:

<%= Html.ActionLink("LinkText", "Action", "Controller", new { id = ""}) %>

这应该手动擦除 id 参数。

The problem is the built in methods take input from the URL you are currently on as well as what you supply. You could try this:

<%= Html.ActionLink("LinkText", "Action", "Controller", new { id = ""}) %>

That should manually wipe the id parameter.

霞映澄塘 2024-07-24 12:46:06

不知道为什么,但它对我不起作用(可能是因为 Mvc2 RC)。 创建了 urlhelper 方法 =>

 public static string
            WithoutRouteValues(this UrlHelper helper, ActionResult action,params string[] routeValues)
        {
            var rv = helper.RequestContext.RouteData.Values;
            var ignoredValues = rv.Where(x=>routeValues.Any(z => z == x.Key)).ToList();
            foreach (var ignoredValue in ignoredValues)
                rv.Remove(ignoredValue.Key);
            var res = helper.Action(action);
            foreach (var ignoredValue in ignoredValues)
                rv.Add(ignoredValue.Key, ignoredValue.Value);
            return res;
        }

Don't know why, but it didn't work for me (maybe because of Mvc2 RC). Created urlhelper method =>

 public static string
            WithoutRouteValues(this UrlHelper helper, ActionResult action,params string[] routeValues)
        {
            var rv = helper.RequestContext.RouteData.Values;
            var ignoredValues = rv.Where(x=>routeValues.Any(z => z == x.Key)).ToList();
            foreach (var ignoredValue in ignoredValues)
                rv.Remove(ignoredValue.Key);
            var res = helper.Action(action);
            foreach (var ignoredValue in ignoredValues)
                rv.Add(ignoredValue.Key, ignoredValue.Value);
            return res;
        }
不再让梦枯萎 2024-07-24 12:46:06

如果您不知道需要显式覆盖哪些值,或者您只是想避免额外的参数列表,则可以使用如下扩展方法。

<a href="@Url.Isolate(u => u.Action("View", "Person"))">View</a>

实施细节在此博文中

If you either don't know what values need to be explicitly overridden or you just want to avoid the extra list of parameters you can use an extension method like the below.

<a href="@Url.Isolate(u => u.Action("View", "Person"))">View</a>

The implementation details are in this blog post

咋地 2024-07-24 12:46:06

我明确将操作名称设置为“Action/”。 看起来有点像黑客,但这是一个快速修复。

@Html.ActionLink("Link Name", "Action/", "Controller")

I explicitly set the action name as "Action/". Seems a little like a hack but it's a quick fix.

@Html.ActionLink("Link Name", "Action/", "Controller")
一影成城 2024-07-24 12:46:06

另一种方法是使用 ActionLink(HtmlHelper, String, String, RouteValueDictionary) 重载,则不需要在最后一个参数中放置 null

<%= Html.ActionLink("Details", "Details", "Product", new RouteValueDictionary(new { id=item.ID })) %>

Another way is to use ActionLink(HtmlHelper, String, String, RouteValueDictionary) overload, then there are no need to put null in the last parameter

<%= Html.ActionLink("Details", "Details", "Product", new RouteValueDictionary(new { id=item.ID })) %>
柏林苍穹下 2024-07-24 12:46:06

Html.ActionLink 的重载在 MVC 的更高版本上发生了更改。 在 MVC 5 及更高版本上。 执行此操作的方法如下:

@Html.ActionLink("LinkText", "Action", "Controller", new { id = "" }, null)

注意,我为 id 参数传递了“”,为 HTMLATTRIBUTES 传递了 null。

The overloads of Html.ActionLink are changed on the later versions of MVC. On MVC 5 and above. This is how to do this:

@Html.ActionLink("LinkText", "Action", "Controller", new { id = "" }, null)

Note I passed "" for id parameter and null for HTMLATTRIBUTES.

跨年 2024-07-24 12:46:06

我需要我的菜单链接是动态的。 我没有为每个页面实现大量额外的代码和路由,而是简单地省去了 HTML 帮助器。

<a href="@(item.websiteBaseURL)/@(item.controller)/@(item.ViewName)">@item.MenuItemName</a>

I needed my menu links to be dynamic. Rather than implement a lot of extra code and routing for every single page I simple dispensed with the HTML helper.

<a href="@(item.websiteBaseURL)/@(item.controller)/@(item.ViewName)">@item.MenuItemName</a>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文