How to Make Language Specific Url in Asp.Net Project?

2.1k views Asked by At

I used westwind globalization library http://west-wind.com/westwind.globalization/ on my project to implement multiple languages. Everything is working fine. Only one problem is that I get culture reference variable at the end of url as http://localhost:18106/LangTest?LocaleId=en.

I want culture reference variable in between domain and page name like http://localhost:18106/en/LangTest.

Acutally I want to do like Microsoft web site http://www.microsoft.com/en-us/default.aspx for example.

1

There are 1 answers

1
Dmitry Kazakov On BEST ANSWER

You need to use IRouteHandler and you need to create custom routes.

By default, Web Forms use file system request handling. For example the MyWebsite/contact.aspx request searches for the contact.aspx file located under the root menu. MyWebsite/superheroes/superheroes.aspx should look for the superheroes.aspx file located inside the superheroes file under the root menu.

Using the global.asax file you may add our own handling. All you need is to call a method, having a RouteCollection parameter, from within the Application_Start method. So you need to create a method called RegisterRoutes and place it within a new file called MyRouteConfig.cs

Here is example:

using System;
using Microsoft.AspNet.FriendlyUrls;
using System.Web.Routing;
using System.Web;
using System.Web.UI;
using System.Web.Compilation;


public static class MyRouteConfig
{
   public static void RegisterRoutes(RouteCollection routes)
   {
        routes.EnableFriendlyUrls();     
        routes.MapPageRoute("superheroes", "superhero/{SuperheroName}", "~/superheroes.aspx");
        routes.MapPageRoute("languageSuperheroes", "{language}/superhero/{SuperheroName}", "~/superheroes.aspx");     
        routes.Add(new System.Web.Routing.Route("{language}/{*page}", new LanguageRouteHandler()));
  } 

   public class LanguageRouteHandler : IRouteHandler
   {
       public IHttpHandler GetHttpHandler(RequestContext requestContext)
       {
           string page = CheckForNullValue(requestContext.RouteData.Values["page"]);
           string virtualPath = "~/" + page;

           if (string.IsNullOrEmpty(page))
           {
               string language= CheckForNullValue(requestContext.RouteData.Values["language"]);
               string newPage = "/home";

               if (!string.IsNullOrEmpty(language))
                   newPage = "/" + language + newPage;
               HttpContext.Current.Response.Redirect(newPage, false);
               HttpContext.Current.Response.StatusCode = 301;
               HttpContext.Current.Response.End();

              //Otherwise, route to home
              //page = "home";
           }

          if (!virtualPath.Contains(".aspx"))
                virtualPath += ".aspx";

           try
           {
               return BuildManager.CreateInstanceFromVirtualPath(virtualPath, typeof(Page)) as IHttpHandler;
           }
           catch
           {
               return null;
           }
       }
   }
}

Please have a look to this article: http://dotnethints.com/blogs/localization-using-routing-system-on-a-web-forms-project-