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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/780643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-07 23:31:32  来源:igfitidea点击:

asp.net mvc Html.ActionLink() keeping route value I don't want

asp.net-mvcactionlink

提问by codette

I have the following ActionLink in my view

我认为有以下 ActionLink

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

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

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

Say I add an ID at the end like so: http://mywebsite.com/Controller/Action/53and 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:

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

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

http://mywebsite.com/Controller/Action/53(注意添加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)

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

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

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

回答by codette

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

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

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

回答by Brian Cauthon

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:

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

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

Then instead of ActionLink to create those links use:

然后,而不是 ActionLink 创建这些链接使用:

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

回答by Garry Shutler

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:

问题是内置方法从您当前所在的 URL 以及您提供的 URL 获取输入。你可以试试这个:

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

That should manually wipe the id parameter.

那应该手动擦除 id 参数。

回答by Arnis Lapsa

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

不知道为什么,但它对我不起作用(可能是因为 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;
        }

回答by Steve Greatrex

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

实现细节在这篇博文中

回答by BorgRebel

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")

回答by Murilo Lima

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

另一种方法是使用ActionLink(HtmlHelper, String, String, RouteValueDictionary) 重载,那么最后一个参数就不需要放null了

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

回答by yogihosting

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

在更高版本的 MVC 上更改了 Html.ActionLink 的重载。在 MVC 5 及更高版本上。这是如何做到这一点:

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

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

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

回答by nuander

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.

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

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