ASP.net MVC3 多语言路由重写

发布于 2024-10-29 06:07:36 字数 341 浏览 1 评论 0原文

有没有什么好的方法来为多语言 Web 应用程序创建路由重写?


URL 架构应为以下

http:///{Language}/{Controller}/{Action}/{Id}

,但也应支持不带语言部分的 URL,但它们不应该直接映射到控制器,而应该生成重定向响应。

这里重要的是,重定向不应该被硬编码为特定语言,而应该根据用户首选语言(如果可能)等因素来确定。

注意:确定正确语言的过程不是问题,只是如何进行非静态重写。

谢谢

Is there any good method to create route rewriting for a multilingual web application?


The URL schema should be the following

http://<Domainname>/{Language}/{Controller}/{Action}/{Id}

but URLs without the Language part should also be supported, but they should not just map to the controllers directly but generate a redirect response.

The important thing here is that the redirect should not be hard coded to a specific language but be determined based on factors like the users preferred language if possible.

Note: The process of determining the correct language is not the problem, just how to do the non static rewriting.

Thanks

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

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

发布评论

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

评论(1

彻夜缠绵 2024-11-05 06:07:36

我通过以下路线做到了这一点;

    routes.MapRoute(
            "Default", // Route name
            "{language}/{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", language = "tr", id = UrlParameter.Optional }, // Parameter defaults
            new { language = @"(tr)|(en)" }
        );

我通过重写 DefaultControllerFactory 的 GetControllerInstance() 方法来处理区域性。示例如下;

public class NinjectControllerFactory : DefaultControllerFactory {

protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType) {

    //Get the {language} parameter in the RouteData

    string UILanguage;

    if (requestContext.RouteData.Values["language"] == null) {

        UILanguage = "tr";
    }
    else {

        UILanguage = requestContext.RouteData.Values["language"].ToString();
    }

    //Get the culture info of the language code
    CultureInfo culture = CultureInfo.CreateSpecificCulture(UILanguage);
    Thread.CurrentThread.CurrentCulture = culture;
    Thread.CurrentThread.CurrentUICulture = culture;

    return base.GetControllerInstance(requestContext, controllerType);
}

}

并将其注册到global.asax;

protected void Application_Start() {

    //other things here


    ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory());
}

I managed that with following routes;

    routes.MapRoute(
            "Default", // Route name
            "{language}/{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", language = "tr", id = UrlParameter.Optional }, // Parameter defaults
            new { language = @"(tr)|(en)" }
        );

I handle the culture by overriding the GetControllerInstance() method of DefaultControllerFactory. the example is below;

public class NinjectControllerFactory : DefaultControllerFactory {

protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType) {

    //Get the {language} parameter in the RouteData

    string UILanguage;

    if (requestContext.RouteData.Values["language"] == null) {

        UILanguage = "tr";
    }
    else {

        UILanguage = requestContext.RouteData.Values["language"].ToString();
    }

    //Get the culture info of the language code
    CultureInfo culture = CultureInfo.CreateSpecificCulture(UILanguage);
    Thread.CurrentThread.CurrentCulture = culture;
    Thread.CurrentThread.CurrentUICulture = culture;

    return base.GetControllerInstance(requestContext, controllerType);
}

}

and register it on the global.asax;

protected void Application_Start() {

    //other things here


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