0

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
        );

    }

1 Answer 1

2
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 :)

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

Comments

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.