如何路由的多语言网址用MVC [英] How to route a multiple language URL with a MVC
问题描述
我需要现有的控制器的多语言网址路线。让我来解释更多:
I need multi-language URL route of existing controller. Let me explain more:
我有一个名为产品是一个控制器和一个名为软件查看;因此,在默认情况下,如果用户输入http://mysite.com/en/Product/Software,得到正确的内容(即确实存在于 http://mysite.com/Product/Software )
I have a controller with name "Product" and View with name "Software"; therefore, by default if the user enters "http://mysite.com/en/Product/Software", get right content (that really exists in http://mysite.com/Product/Software),
但是,如果另一个用户 - 法国用户 - 类型http://mysite.com/fr/Produits/logiciels,必须把上面的控制器,并与正确的内容显示(相同的http://mysite.com/Product/Software 但法文本)。
However, if another user -- a French user -- types "http://mysite.com/fr/Produits/logiciels", must get above controller and show with right content (same http://mysite.com/Product/Software but with French text).
请注意:我设定{语言} / {控制器} / {行动} / {ID}
Note: I set the route table with "{language}/{controller}/{action}/{id}"
任何其他无效的URL必须显示404页。
Any other invalid URL must show the 404 page.
这可能吗?
推荐答案
在丹的邮政大楼,我下使用的翻译我的控制器和动作的名称。
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.
(我的国际化code在很大程度上是由这个伟大的博客文章为主。
<一href=\"http://afana.me/post/aspnet-mvc-internationalization-part-2.aspx\">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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!