我翻译了我的mvc网站,这个网站运行得很好.如果我选择其他语言(荷兰语或英语),内容将被翻译.这是有效的,因为我在会话中设置了文化.
现在我想在网址中显示所选文化(=文化).如果它是默认语言,则不应在网址中显示,只有当它不是默认语言时才应在网址中显示.
例如:
对于默认文化(荷兰语):
site.com/foo
site.com/foo/bar
site.com/foo/bar/5
Run Code Online (Sandbox Code Playgroud)
对于非默认文化(英语):
site.com/en/foo
site.com/en/foo/bar
site.com/en/foo/bar/5
Run Code Online (Sandbox Code Playgroud)
我的问题是我总是看到这个:
site.com/ nl/foo/bar/5即使我点击了英文(参见_Layout.cs).我的内容是用英文翻译的,但网址中的路由参数仍然是"nl"而不是"en".
我怎样才能解决这个问题或者我做错了什么?
我尝试在global.asax中设置RouteData但没有帮助.
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("favicon.ico");
routes.LowercaseUrls = true;
routes.MapRoute(
name: "Errors",
url: "Error/{action}/{code}",
defaults: new { controller = "Error", action = "Other", code = RouteParameter.Optional }
);
routes.MapRoute(
name: "DefaultWithCulture",
url: "{culture}/{controller}/{action}/{id}",
defaults: new { culture = "nl", controller = "Home", action = "Index", id = UrlParameter.Optional },
constraints: new { culture = "[a-z]{2}" } …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用路由实现本地化
我有以下内容:
routes.MapRoute( "DefaultLocalized",
"{lang}/{controller}/{action}/{id}",
new { controller = "Home",
action = "Index",
id = "",
lang = "en" }
);
routes.MapRoute( "Default",
"{controller}/{action}/{id}",
new { controller = "Home",
action = "Index",
id = "" }
);
Run Code Online (Sandbox Code Playgroud)
当我调用我的页面时domain/en/home/index,它工作正常,但当我打电话时,domain/home/index我得到错误404:无法找到资源.
此外,当我在domain/en/home/index我点击一个安全的页面时,我被重定向到domain/Account/login如何重定向到domain/en/Account/login?
另外,当我收到应用程序错误时,如何将其重定向到domain/en/home/error?
真正的问题是如何使用语言作为路由参数实现本地化?