asp.net mvc Html.ActionLink() 保留我不想要的路由值
我的视图中有以下 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
解决办法是指定我自己的路由值(下面第四个参数)
The solution is to specify my own route values (the fourth parameter below)
听起来您需要注册第二个“仅操作”路由并使用 Html.RouteLink()。 首先在应用程序启动时注册这样的路由:
然后使用以下命令代替 ActionLink 来创建这些链接:
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:
Then instead of ActionLink to create those links use:
问题是内置方法从您当前所在的 URL 以及您提供的内容获取输入。 您可以尝试以下操作:
这应该手动擦除 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:
That should manually wipe the id parameter.
不知道为什么,但它对我不起作用(可能是因为 Mvc2 RC)。 创建了 urlhelper 方法 =>
Don't know why, but it didn't work for me (maybe because of Mvc2 RC). Created urlhelper method =>
如果您不知道需要显式覆盖哪些值,或者您只是想避免额外的参数列表,则可以使用如下扩展方法。
实施细节在此博文中
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.
The implementation details are in this blog post
我明确将操作名称设置为“Action/”。 看起来有点像黑客,但这是一个快速修复。
I explicitly set the action name as "Action/". Seems a little like a hack but it's a quick fix.
另一种方法是使用 ActionLink(HtmlHelper, String, String, RouteValueDictionary) 重载,则不需要在最后一个参数中放置 null
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 的重载在 MVC 的更高版本上发生了更改。 在 MVC 5 及更高版本上。 执行此操作的方法如下:
注意,我为 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:
Note I passed "" for id parameter and null for HTMLATTRIBUTES.
我需要我的菜单链接是动态的。 我没有为每个页面实现大量额外的代码和路由,而是简单地省去了 HTML 帮助器。
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.