0

How we can add language name in URL by using Routing?

my site runs on http://localhost:41213/default.aspx URL successfully but this site in multilingual and my client wants run this site according to language like he wants http://localhost:41213/en/default.aspx instead of http://localhost:41213/default.aspx URL.

So my problem is that how to add en,es,hi,etc in URL and how to read this? default.aspx page is on root directory and it is home page.

2 Answers 2

1

Use this code in global.asax

public static void RegisterRoutes(System.Web.Routing.RouteCollection routes)
    {
        routes.Add(new System.Web.Routing.Route("{language}/{*page}", new CustomRouteHandler()));
    }
void Application_Start(object sender, EventArgs e)
    {
        RegisterRoutes(System.Web.Routing.RouteTable.Routes);
    }
void Application_BeginRequest(object sender, EventArgs e)
    {
        string URL = HttpContext.Current.Request.Url.PathAndQuery;
        string language = TemplateControlExtension.Language;
        if (URL.ToLower() == "/default.aspx")
        {
            HttpContext.Current.Response.Redirect("/" + language + URL);
        }
    }

make a router handler class like this...

public class CustomRouteHandler : IRouteHandler
{
    public IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        string language = TemplateControlExtension.GetString(null, requestContext.RouteData.Values["language"]).ToLower();
        string page = TemplateControlExtension.GetString(null, requestContext.RouteData.Values["page"]).ToLower();

        if (string.IsNullOrEmpty(page))
        {
            HttpContext.Current.Response.Redirect("/" + language + "/default.aspx");
        }

        string VirtualPath = "~/" + page;

        if (language != null)
        {
            if (!VIPCultureInfo.CheckExistCulture(language))
            {
                HttpContext.Current.Response.Redirect("/" + SiteSettingManager.DefaultCultureLaunguage + "/default.aspx");
            }
            TemplateControlExtension.Language = language;
        }
        try
        {
            if (VirtualPath.Contains(".ashx"))
            {
                return (IHttpHandler)BuildManager.CreateInstanceFromVirtualPath(VirtualPath, typeof(IHttpHandler));
            }
            return BuildManager.CreateInstanceFromVirtualPath(VirtualPath, typeof(Page)) as IHttpHandler;
        }
        catch
        {
            return null;
        }
    }
}

By using this i hope your requirement has fulfill.....

Sign up to request clarification or add additional context in comments.

1 Comment

I am trying using this code in my scenerio, but getting error on "TemplateControlExtension" and "BuildManager", could you please pass the references of these class. Thanks in advance
0

Probably the best way to do this is to have an initial page where he chooses the language he wants. Then the site loads a cookie to his browser indicating his language preference. In subsequent visits, your site reads the cookie and automatically takes him to his language preference.

6 Comments

I did this and this works fine but my client wants like msdn.microsoft.com/en-US according to this /en-US/ add with this URL so he want like this, because this is too much SEO friendly.
Just clarifying: your client wants to type /en-US/ in the URL instead of selecting "US English" from a set of options on a page?
He said when we choose any other language in that case URL has been changed and URL takes new culture info so he wants this same functionality and this can be possible by routing but how?
It sounds as if you're asking how to set up a multilingual web site. If so, there are multiple ways, but one way is to duplicate content in different paths. One path for English, one for Spanish, etc. This makes it very difficult to maintain content, however.
A more sophisticated approach is to have all language content served from a database that has entries for each message/language combination.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.