如何进行文化参数作为URL路由参数不是请求参数? [英] how to carry culture parameter in url as routing parameter not request parameter?

查看:134
本文介绍了如何进行文化参数作为URL路由参数不是请求参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个自定义的路由系统ASP.NET MVC 4项目,使我的网站多语种。我想继续像本地主机/ EN / controllerName / actionName URL文化参数。但问题是,当我写例如本地主机/家用路由引擎设家为文化名。我怎样才能解决这个问题?

I want to create a custom routing system ASP.NET MVC 4 project to make my website multilingual. and I want to carry culture parameter in url like localhost/en/controllerName/actionName. but the problem is when I write for example localhost/home routing engine supposes "home" as culture name. How can I solve this problem?

推荐答案

如果您所有的航线将实施文化参数,可以使用类似:

If all your routes will enforce culture parameter, you can use something like:

routes.MapRoute(
    name: "LocalizedDefault",
    url: "{lang}/{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
    constraints: new { lang = @"(es|en|fr)" }
);

如果你仍然想为本地主机的/ home 回应,只是让默认路由上面的那个,这样,由于限制的第一个将不匹配后回落到第二个:

If you still want to respond to localhost/home, just let the default route after the one above, that way the first one will not match because of the constraints and fall back to the second:

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

这篇关于如何进行文化参数作为URL路由参数不是请求参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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