开发者

ASP.NET MVC 3.0 Root Redirect for Localized Web Site

Actualy i handle globalization for my ASP.NET MVC 3.0 Web Site by passing culture in the url like

http://mysite.com/en-US/

I use the following Route in Global.asax

            routes.MapRoute("Default", "{language}-{culture}/{controller}/{action开发者_运维问答}/{id}",
                        new
                            {
                                controller = "Home",
                                action = "Index",
                                id = UrlParameter.Optional,
                                language = "fr",
                                culture = "FR"
                            });

Now i need to redirect users accessing the root of the web site http://mysite.com to the default culture or default browser culture http://mysite.com/en-US.

Actually users got a 404 error when acessing the root url without culture specification.

How can i perform this ?

Thanks.


I think you really need two routes, in the long form of the route you need a constraint to avoid the routes clashing (actually you may not since you are using a dash "-", as long as you have no dashes in controller names). Something like this:-

routes.MapRoute("Default", "{language}-{culture}/{controller}/{action}/{id}",
    new
        {
            controller = "Home",
            action = "Index",
            id = UrlParameter.Optional,
        }
    ,
    new
        {
            language = @"[a-z][a-z]|^",
            culture = @"[A-Z][A-Z]|^"
        });

routes.MapRoute("DefaultLocale", "{controller}/{action}/{id}",
    new
        {
            controller = "Home",
            action = "Index",
            id = UrlParameter.Optional,
            language = "fr",
            culture = "FR"
        });
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜