开发者

How to URL route based on UICulture settings?

I have ASP.NET 4 project (not MVC). I 开发者_如何学JAVAneed to create url route based on user input language.

Project has only two languages "he" and "en".

User can enter the site and if his culture is set to anything besides he-IL i want to re-route him to website.com/en/ otherwise to website.com/he/

Default.aspx should remain same page which uses Globalization features translate values based on user's culture settings in browser.

How can i do that? what should i do besides writing a route in Global.asax and How to write this route.


This shouldnt be hard. Yes the Global.ascx is the best place to start.

First map the Routes,

    protected void RegisterRoutes(RouteCollection routes)
    {
        //Contact route for EN culture
        routes.MapPageRoute(
            "contactRouteEN",
            "en/contact",
            "~/Contact.aspx"
        );

        routes.MapPageRoute(
            "contactRouteHE",
            "he/contact",
            "~/Contact.aspx"
        );
    }

    protected void Application_Start(object sender, EventArgs e)
    {
        RegisterRoutes(RouteTable.Routes);
    }

That much establishes the routes.

The problem your describing sounds more like a Globalization issue than url routing problem. The url portion of the issue will be cosmetic to the user but won't attack the underlying issue in my view. ASP.Net provides facilities for Globalization. For example you can use LocalResources. To do this for the pages at your applications root level (not nested inside folders) Right click the website and choose Add ASP.Net Folder Choose App_LocalResources. Right click the App_LocalResources folder and choose Add Item Choose Resource File.

It is important that you name the file according to the culture you plan to target

You can create the first file to be Contact.aspx.resx to be the default resource file (maybe english?)

ASP.Net will try to find the most specific culture to match the resource files to and will resort to the default if a more specific is not provided.

The naming convention follows PageName.aspx.languageID-cultureId.resx

You could have Contact.aspx.he.resx

In a label control for example you could set it like this

<asp:Label ID="lbContactMessage" runat="server" Text="something" meta:resourcekey="yourmatchingkeyfromresourcefile"></asp:Label>

For more info see http://msdn.microsoft.com/en-us/library/c6zyy3s9.aspx


Seems like you are trying to do something unintended with Routing.

If the language in URL does nothing and you need for it to appear in URL only then you either make hackish solution with HTTP modules rewriting urls to remove it and add back to generated html or simply map the same site to two virtual folders /en and /he in IIS and make a simple Default.aspx page at / to redirect to appropriate one based on user culture.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜