Laguna
Laguna

Reputation: 3876

Route not working - ASP.NET MVC 3 Localization locale in URL

I am localizing my MVC3 site based on the route data. for example, http://domain/fr should display the site in French and http://domain should default to english...below is how i registered my routes in Global.ascx.

My issue is that http://domain/fr/Home/Index will work, but http://domain/Home/Index will display resource not found error, and with an investigation it tells me the route table is mapping "Home" to {lang}

What am I missing?

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

        routes.IgnoreRoute("favicon.ico");

        routes.MapRoute(
            "LogOn", // Route name
            "Account/{action}", // URL with parameters
            new { controller = "Account", action = "LogOn" } // Parameter defaults
        );

        routes.MapRoute(
            "Localization", // Route name
            "{lang}/{controller}/{action}", // URL with parameters
            new { UrlParameter.Optional, controller = "Home", action = "Index"} // Parameter defaults
        );

        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}", // URL with parameters
            new { controller = "Home", action = "Index"} // Parameter defaults
        );

    }

Upvotes: 0

Views: 751

Answers (1)

bedane
bedane

Reputation: 1127

routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}", // URL with parameters
            new { controller = "Home", action = "Index"}, // Parameter defaults
            new { controller = "[a-zA-Z]{3,}" } //regexp constraint on controller name
        );

routes.MapRoute(
            "Localization", // Route name
            "{lang}/{controller}/{action}", // URL with parameters
            new { UrlParameter.Optional, controller = "Home", action = "Index"} // Parameter defaults
        );

Should do the trick, provided all your controller names are longer than 2 characters :)

Upvotes: 2

Related Questions