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

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

问题描述

该网站我的工作有一些相当复杂的路由结构,我们遇到了一些困难与路由引擎打造的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的方法的一个恼人的特征问题:如果指向同一个控制器/动作是否希望他们与否,将保留现有的路由值。我们preFER有过什么我们的链接的样子,并且在一些情况下,不能有保留了现有参数控制。一个例子是在这里我们网站的主导航导致不带参数的搜索结果页面设置 - 默认搜索页面,如果你喜欢。我说这是一个恼人的功能,因为它是没有一个明显的扩展点的ASP.Net MVC框架看似口述实现的一个罕见的事例 - 我们将preFER不是创建自定义的ActionLink code编写一个简单的导航链接在我们的母版页!

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的方法值,但如果这是我们唯一的选择,我们将使用它。然而,在present如果在查询字符串显示那么它是作为无用我们作为直接把参数进路线

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?

如果我们确实需要明确排除线路段,我们怎么能prevent从渲染路由作为查询字符串参数的方法?

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

干杯,
扎克

Cheers, Zac

推荐答案

设置路由值在调用时为空或空字符串 Html.ActionLink Html.RouteLink (或任何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航线假设你在首页/索引/ 123。如果你调用 Html.RouteLink(新{ID = 456})然后MVC会注意到控制器=家中的环境路由值行动=指数。它还会注意到 ID =123,但将通过明确的456被覆盖的环境路线值。这将导致生成的URL为主页/索引/ 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(新{行动=关于})。 左右的行动将覆盖当前指数行动,和ID参数将得到完全清除了!但是,为什么,你问?因为一旦你一个无效参数部分再经过这一切参数段就会无效。在这种情况下,动作,是由一个新的显式值失效,从而为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(新{行动=})则生成的URL将只是家,因为你的无效 导致用一个空字符串,然后在ID行动无效也因为它的失效后,来到行动。

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天全站免登陆