language code as parameter in url

Apr 11, 2012 at 4:21 PM

Hi everyone!

I have a successfully converted nerddinner to a multilanguage site. Now I want to improve the site for better google indexing. The users are able to select prefered language using a drop down list. There are x numbers of languages to chose between. 

I would like that when a users has chosen prefered language, then the language code should be attached to the url. This should ofcourse change if the users decides to change the language. 

My modyfied Gloabl.asax file looks like this

       public void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
                "PrettyDetails",
                "{Id}",
                    new { controller = "Dinners", action = "Details" },
                    new { Id = @"\d+" }
                );


        routes.MapRoute(
                "UpcomingDinners",
                "Dinners/Page/{page}",
                new { controller = "Dinners", action = "Index" }
                );

		routes.MapRoute( 
				"Default", // Route name 
				"{controller}/{action}/{id}/{language}", // URL with parameters 
				new { controller = "Home", action = "Index", id = "", language="en" }, // Parameter defaults
				new { id = @"\d+" }
		);

        routes.MapRoute(
            "OpenIdDiscover",
            "Auth/Discover"
            );
    }

Note: the language code will be in following format, es, en, us, sw and so forth. I will not be using culture values like es-BO, en-GB, en-US... 

My HomeController looks like this

public class HomeController : Controller 
{
        public ActionResult Index(int id, string language)
        {
        //do something... 
        } 
} 

string language is ofcourse etc; en, cz, es...

However running this return following error. "The incoming request does not match any route."

The error comes from Default.aspx.cs file and the code goes:

        public void Page_Load(object sender, System.EventArgs e) 
        {
            HttpContext.Current.RewritePath(Request.ApplicationPath, false);
            IHttpHandler httpHandler = new MvcHttpHandler();
            httpHandler.ProcessRequest(HttpContext.Current);
        }

 

Any suggestions!

Thanks.