如何阻止 ASP.Net MVC Html.ActionLink 使用现有的路由值? [英] How Can I Stop ASP.Net MVC Html.ActionLink From Using Existing Route Values?

查看:13
本文介绍了如何阻止 ASP.Net MVC Html.ActionLink 使用现有的路由值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理的网站有一些相当复杂的路由结构,我们在使用路由引擎以我们需要构建 URL 的方式构建 URL 时遇到了一些困难.

The website I'm working on has some fairly complicated routing structures and we're experiencing some difficulties working with the routing engine to build URLs the way we need them to be built.

我们有一个搜索结果页面,它使用基于 RegEx 的模式匹配将多个变量分组到单个路由段中(即www.host.com/{structuralParameters}"可以如下:www.host.com/variableA-variableB-variableC" - 其中变量 A 到 C 都是可选的).经过一些工作,这对我们来说很好.

We have a search results page that uses RegEx based pattern matching to group several variables into a single route segment (i.e. "www.host.com/{structuralParameters}" can be the following: "www.host.com/variableA-variableB-variableC" - where variables A through C are all optional). This is working for us fine after a bit of work.

我们遇到的问题解决了 ActionLink 方法的一个烦人的特性:如果你指向同一个控制器/动作,无论你是否想要它们,它都会保留现有的路由值.我们更愿意控制链接的外观,并且在某些情况下,不能保留现有参数.一个例子是我们网站的主导航指向一个没有设置参数的搜索结果页面 - 如果您愿意,可以使用默认搜索页面.我说这是一个令人讨厌的功能,因为它是 ASP.Net MVC 框架的一个罕见实例,似乎没有明显的扩展点就指定实现 - 我们不希望创建自定义 ActionLink 代码来在我们的母版页中编写简单的导航链接!

The problem we are experiencing resolves around an annoying feature of the ActionLink method: if you point to the same controller/action it will retain the existing route values whether you want them or not. We prefer to have control over what our links look like and, in some cases, cannot have the existing parameters retained. An example would be where our site's main navigation leads to a search results page with no parameters set - a default search page, if you like. I say this is an annoying feature because it is a rare instance of the ASP.Net MVC Framework seemingly dictating implementation without an obvious extension point - we would prefer not to create custom ActionLink code to write a simple navigation link in our master page!

我看到有人说您需要将此类参数显式设置为空字符串,但是当我们尝试这样做时,它只是将参数从路由值更改为查询字符串参数.在我看来,我们应该被要求明确排除我们没有作为参数明确传递给 ActionLink 方法的值,但如果这是我们唯一的选择,我们将使用它.但是目前如果它显示在查询字符串中,那么它对我们来说就像直接将参数放入路由中一样无用.

I've seen some say that you need to explicitly set such parameters to be empty strings but when we try this it just changes the parameters from route values into query string parameters. It doesn't seem right to me that we should be required to explicitly exclude values we aren't explicitly passing as parameters to the ActionLink method but if this is our only option we will use it. However at present if it is displaying in the query string then it is as useless to us as putting the parameters directly into the route.

我知道我们的路由结构加剧了这个问题 - 如果我们使用更简单的方法(即 www.host.com/variableA/variableB/variableC),我们可能不会遇到任何问题,但我们的 URL 结构是不可协商的- 它旨在满足与可用性、搜索引擎优化和链接/内容共享相关的非常具体的需求.

I'm aware that our routing structure exasperates this problem - we probably wouldn't have any issue if we used a simpler approach (i.e. www.host.com/variableA/variableB/variableC) but our URL structure is not negotiable - it was designed to meet very specific needs relating to usability, SEO, and link/content sharing.

我们如何使用 Html.ActionLink 生成页面链接而不依赖于当前的路由数据(或者,如果可能,需要明确排除路由段),即使这些链接导致相同的操作方法?

How can we use Html.ActionLink to generate links to pages without falling back on the current route data (or, if possible, needing to explicitly excluding route segments) even if those links lead to the same action methods?

如果我们确实需要明确排除路由段,我们如何防止该方法将路由呈现为查询字符串参数?

If we do need to explicitly exclude route segments, how can we prevent the method from rendering the routes as query string parameters?

这个看似很小的问题却给我们带来了极大的悲痛,我将感谢您为解决它提供的任何帮助.

This seemingly small problem is causing us a surprising amount of grief and I will be thankful for any help in resolving it.

按照 LukLed 的要求,这是一个 ActionLink 调用示例:

As requested by LukLed, here's a sample ActionLink call:

// I've made it generic, but this should call the Search action of the 
// ItemController, the text and title attribute should say "Link Text" but there
// should be no parameters - or maybe just the defaults, depending on the route.
// 
// Assume that this can be called from *any* page but should not be influenced by
// the current route - some routes will be called from other sections with the same
// structure/parameters.
Html.ActionLink( 
    "Link Text",
    "Search", 
    "Item", 
    new { }, 
    new { title = "Link Text" } 
);

推荐答案

在调用Html.ActionLinkHtml.RouteLink时设置路由值为null或空字符串(或任何 URL 生成方法)将清除环境"路由值.

Setting route values to be null or empty string when calling Html.ActionLink or Html.RouteLink (or any URL generation method) will clear out the "ambient" route values.

例如,使用标准 MVC 控制器/动作/id 路由,假设您位于Home/Index/123".如果你调用 Html.RouteLink(new { id = 456 }) 那么 MVC 会注意到 controller="Home"action= 的环境"路由值索引".它还会注意到 id="123" 的环境路由值,但会被显式的456"覆盖.这将导致生成的 URL 为Home/Index/456".

For example, with the standard MVC controller/action/id route suppose you're on "Home/Index/123". If you call Html.RouteLink(new { id = 456 }) then MVC will notice the "ambient" route values of controller="Home" and action="Index". It will also notice the ambient route value of id="123" but that will get overwritten by the explicit "456". This will cause the generated URL to be "Home/Index/456".

参数的顺序也很重要.例如,假设您调用了 Html.RouteLink(new { action = "About" }).关于"动作将覆盖当前的索引"动作,而id"参数将被完全清除!但你问为什么?因为一旦你使一个参数段失效,那么它之后的所有参数段都将失效.在这种情况下,action"被一个新的显式值无效,因此紧随其后且没有显式值的id"也被无效.因此,生成的 URL 将只是Home/About"(没有 ID).

The ordering of the parameters matters as well. For example, say you called Html.RouteLink(new { action = "About" }). The "About" action would overwrite the current "Index" action, and the "id" parameter would get cleared out entirely! But why, you ask? Because once you invalidate a parameter segment then all parameter segments after it will get invalidated. In this case, "action" was invalidated by a new explicit value so the "id", which comes after it, and has no explicit value, also gets invalidated. Thus, the generated URL would be just "Home/About" (without an ID).

在同样的场景中,如果您调用 Html.RouteLink(new { action = "" }) 那么生成的 URL 将只是Home",因为您使用空字符串使action"无效,然后导致id"也无效,因为它出现在无效的action"之后.

In this same scenario if you called Html.RouteLink(new { action = "" }) then the generated URL would be just "Home" because you invalidated the "action" with an empty string, and then that caused the "id" to be invalidated as well because it came after the invalidated "action".

这篇关于如何阻止 ASP.Net MVC Html.ActionLink 使用现有的路由值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆