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

查看:21
本文介绍了如何阻止 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.

我们有一个搜索结果页面,它使用基于正则表达式的模式匹配将多个变量分组到一个路由段中(即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 将只是主页/关于"(没有 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天全站免登陆