Asp.net mvc 3 - 按名称获取参数

发布于 2024-12-18 06:19:30 字数 386 浏览 2 评论 0原文

有一个 url

http://mysite.com/en-us/articles/browse/en-us/12/byCategory

已知这些参数之一名为 culture。当然,我可以调用 ViewContext.RouteData.Values["culture"] 来获取 culturevalue,但它没有说我将其中的 en-us 参数(第一个或第二个)命名为 culture

我的任务是识别哪些参数名为 culture,将其替换为指定值并返回新的 url。怎么做呢?

There is an url

http://mysite.com/en-us/articles/browse/en-us/12/byCategory

It's known that one of these parameters named culture. Of course I can call ViewContext.RouteData.Values["culture"] to get the value of the culture, but it doesn't say me which en-us parameter (first or second) named culture.

My task is recognize which of parameters named culture, replace it with specified value and return the new url. How to do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巡山小妖精 2024-12-25 06:19:30

您不能有两个同名的段参数

因此,如果您说ViewContext.RouteData.Values["culture"],您将获得与特定相关的正确区域性值/em> 分段第一个或第二个,具体取决于您的路由 URL 定义,其可能如下所示:

{culture}/{controller}/{action}/{lang}/{id}/{filter}

如您所见,只有一个名为 culture 的分段参数。

注意:上层路由 URL 定义可能与您的不同,但它显示了一个可以与您的请求 URL 配合使用的示例。

获取当前路由的 URL 定义

但是,否则您可以通过以下方式获取当前正在执行的路由 URL 定义来定位正确的段:

((Route)ViewContext.RouteData.Route).Url

RouteBase 转换为 Route 可能在 99% 的情况下有效,因为使用自定义路由类并不常见,但尽管自定义类更有可能从 Route 继承,而不是从 RouteBase 继承。

解析此 URL 定义可能是一项更具挑战性的任务,具体取决于路由定义的复杂性。但您或多或少可以成功地将任何 URL 定义与请求 URL 段进行匹配。这可能正是您所追求的。

让当前路由为您进行替换

但是,您可以让路由本身为您完成此操作,而不是解析路由的 URL 并替换正确的段:

((Route)ViewContext.RouteData.Route)
    .GetVirtualPath(
        ViewContext.RequestContext,
        new RouteValueDictionary(new { culture = "en-GB" }))
    .VirtualPath

You can't have two segment parameters with the same name

So if you say ViewContext.RouteData.Values["culture"] you will get the correct culture value that is related to a specific segment either the first or the second one, depending on your route URL definition which may look like:

{culture}/{controller}/{action}/{lang}/{id}/{filter}

As you can see there's only one segment parameter called culture.

Note: upper route URL definition may not be like yours but it shows an example that could work with your request URL.

Get current route's URL definition

But otherwise you could get the currently executing route URL definition to locate the correct segment by:

((Route)ViewContext.RouteData.Route).Url

Casting RouteBase to Route would probably work in 99% of cases because using custom route classes isn't that common, but even though custom classes are more likely to inherit from Route than RouteBase.

Parsing this URL definition may be a more challenging task depending on the complexity of your route definitions. But you could more or less successfully match any URL definition against request URL segments. And this may be exactly what you're after.

Let current route do the replacement for you

But instead of parsing route's URL and replacing correct segment, you could let route itself do it for you simply by:

((Route)ViewContext.RouteData.Route)
    .GetVirtualPath(
        ViewContext.RequestContext,
        new RouteValueDictionary(new { culture = "en-GB" }))
    .VirtualPath
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文