如何使用 MVC 路由多语言 URL [英] How to route a multiple language URL with a MVC

查看:19
本文介绍了如何使用 MVC 路由多语言 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要现有控制器的多语言 URL 路由.让我解释一下:

I need multi-language URL route of existing controller. Let me explain more:

我有一个名为Product"的控制器和名为Software"的视图;因此,默认情况下,如果用户输入http://example.com/en/Product/Software",获取正确的内容(在 http://example.com/Product/Software 中确实存在),

I have a controller with name "Product" and View with name "Software"; therefore, by default if the user enters "http://example.com/en/Product/Software", get right content (that really exists in http://example.com/Product/Software),

但是,如果另一个用户(法国用户)输入http://example.com/fr/Produits/logiciels",必须高于控制器并显示正确的内容(相同的 http://example.com/Product/Software 但带有法语文本).

However, if another user -- a French user -- types "http://example.com/fr/Produits/logiciels", must get above controller and show with right content (same http://example.com/Product/Software but with French text).

注意:我使用{language}/{controller}/{action}/{id}"设置路由表

Note: I set the route table with "{language}/{controller}/{action}/{id}"

任何其他无效 URL 必须显示 404 页面.

Any other invalid URL must show the 404 page.

有可能吗?

推荐答案

以 Dan 的帖子为基础,我正在使用下面的翻译我的控制器和动作名称.

Building upon Dan's post, I'm using the beneath for translating my controller and action names.

我创建了一个表来存储值,这可以而且可能应该保存在资源文件中以将所有内容放在一起;但是我使用了一个数据库表,因为它更适合我公司的流程.

I created a table to store the values, this could and probably should be held in the resource files to keep everything together; however I used a database table as it works better with my companies processes.

CREATE TABLE [dbo].[RoutingTranslations](
[RouteId] [int] IDENTITY(1,1) NOT NULL,
[ControllerName] [nvarchar](50) NOT NULL,
[ActionName] [nvarchar](50) NOT NULL,
[ControllerDisplayName] [nvarchar](50) NOT NULL,
[ActionDisplayName] [nvarchar](50) NOT NULL,
[LanguageCode] [varchar](10) NOT NULL)

然后将 RouteConfig.cs 文件更改为:

The RouteConfig.cs file was then changed to:

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        //Build up routing table based from the database.  
        //This will stop us from having to create shedloads of these statements each time a new language, controller or action is added
        using (GeneralEntities db = new GeneralEntities())
        {
            List<RoutingTranslation> rt = db.RoutingTranslations.ToList();
            foreach (var r in rt)
            {
                routes.MapRoute(
                    name: r.LanguageCode + r.ControllerDisplayName + r.ActionDisplayName,
                    url: r.LanguageCode + "/" + r.ControllerDisplayName + "/" + r.ActionDisplayName + "/{id}",
                    defaults: new { culture = r.LanguageCode, controller = r.ControllerName, action = r.ActionName, id = UrlParameter.Optional },
                    constraints: new { culture = r.LanguageCode }
                );
            }                
        }

        //Global catchall
        routes.MapRoute(
            name: "Default",
            url: "{culture}/{controller}/{action}/{id}",
            defaults: new {culture = CultureHelper.GetDefaultCulture(), controller = "Default", action = "Index", id = UrlParameter.Optional }
            //defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

    }
}

默认情况下,这将始终使用英文控制器和操作名称,但允许您通过在表中输入值来提供覆盖.

By default this will always use the English controller and action names, but allows you to provide an override by entering the values into the table.

(我的国际化代码主要基于这篇很棒的博文.http://afana.me/post/aspnet-mvc-internationalization-part-2.aspx)

(My internationalization code is largely based from this great blog post. http://afana.me/post/aspnet-mvc-internationalization-part-2.aspx)

这篇关于如何使用 MVC 路由多语言 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆