在每个请求的路由中设置默认值

发布于 2024-11-11 02:50:22 字数 732 浏览 0 评论 0原文

我有以下路由,这基本上使我能够将语言保留在 url 中,并确保只有 defr (约束)是可能的。在 Default - 路由中,如果包含的 url 中没有语言,我将 de 设置为标准:

// Routing with language
routes.MapRoute("Default_with_language", "{lang}/{controller}/{action}/{id}", new
{
    controller = "Home",
    action = "Index",
    id = UrlParameter.Optional,
}, new { lang = "de|fr" });
// Standard-Routing
routes.MapRoute("Default", "{controller}/{action}/{id}", new
{
    controller = "Home",
    action = "Index",
    id = UrlParameter.Optional,
    lang = "de",
});

How can I change the default-language in every request?假设我有两个网址,一个是德语,一个是法语,如果调用的是法语网址,我希望有 lang = "fr"...

谢谢您的任何提示 SL3DG3

I have got following routing, which basically enables me to keep the language within the url and makes sure only de and fr (constraint) is possible. In the Default - Routing, I set de as standard if there is no language in the url contained:

// Routing with language
routes.MapRoute("Default_with_language", "{lang}/{controller}/{action}/{id}", new
{
    controller = "Home",
    action = "Index",
    id = UrlParameter.Optional,
}, new { lang = "de|fr" });
// Standard-Routing
routes.MapRoute("Default", "{controller}/{action}/{id}", new
{
    controller = "Home",
    action = "Index",
    id = UrlParameter.Optional,
    lang = "de",
});

How can I change the default-language in each request? Let's say I have two Urls, one in german and one in french, I would like to have lang = "fr" if the french url is called...

Thx for any tipp
sl3dg3

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

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

发布评论

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

评论(2

牛↙奶布丁 2024-11-18 02:50:22

在您的 Application_BeginRequest 处理程序中,您可以访问当前路由,并且应该能够使用类似的方法更改值。

 HttpContext.Current.Request.RequestContext.RouteData.Values.Add("lang", detectedLanguageFromUrl);

希望这足以使路由获取新值。

In your Application_BeginRequest handler, you can access the current route and you should be able to change the values using something like this.

 HttpContext.Current.Request.RequestContext.RouteData.Values.Add("lang", detectedLanguageFromUrl);

Hopefully, that's enough to make routing pick up the new value.

指尖上得阳光 2024-11-18 02:50:22

@Tim:谢谢你的回答。我同时想出了类似的东西:

var defaultRoute = RouteTable.Routes["Default"] as Route;
if (Request.Url.ToString().Contains("myFrenchUrl.ch"))
    defaultRoute.Defaults["lang"] = "fr";
else
    defaultRoute.Defaults["lang"] = "de";

sl3dg3

@Tim: Thx for your answer. I figured out something similar at the same time:

var defaultRoute = RouteTable.Routes["Default"] as Route;
if (Request.Url.ToString().Contains("myFrenchUrl.ch"))
    defaultRoute.Defaults["lang"] = "fr";
else
    defaultRoute.Defaults["lang"] = "de";

sl3dg3

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文