0

I'm new to ASP.NET MVC and I've encountered problem, which I can't solve. I've spent two days searching Google, but still nothing.

So, my situation is: I'm trying to implement localization in my ASP.NET MVC application using routing.

www.mysite.com/Home/Index - for default (Russian)

www.mysite.com/en/Home/Index - for English

So I'm registering routes:

public static void RegisterRoutes(RouteCollection routes)
        {
            routes.MapRoute(
                        "Default",
                        "{controller}/{action}",
                        new { controller = "Home", action = "Index"},
                        new { controller = "^[a-zA-z]{4,}$" }
             );

            routes.MapRoute(
                        "Localization",
                        "{lang}/{controller}/{action}",
                        new { lang = "en", controller = "Home", action = "Index" }
            );
        }

Navigating to default: www.mysite.com/Home/Index works fine. But when I try to navigate to www.mysite.com/en/Home/Index I get 404 error.

What is more interesting, RouteDebugger does'n see my new route:

RouteDebugger All Routes Table

Adding more new routes, changing the order of their mapping or even deleting all my routes do nothing. It seems that route {controller}/{action}/{id} is just the default route for MVC which was added "before me".

So, what could be the problem? I have totally no idea! I've seen screenshots of Rote Debugger from other sites and their routes are displayed. For example some other site: Screenshot

Maybe I have to change something in web.config? I'm completely confused. Please help! Thank you very much for your time!

  • Sounds like your site hasn't recompiled. are you hosting it in iis Express/IIS/VWD Env? try cleaning your solution and then hitting rebuild, see if that fixes it – Slicksim Apr 19 '13 at 10:16
  • Thank you very much for your answer! I've cleaned the solution (Build -> Clean Solution) and rebuid it (Build -> Rebuild Solution) but still noting. – Алексей Стецюра Apr 19 '13 at 10:21
  • What version of MVC? Where is RegisterRoutes? Is it in the App_Start folder in the RouteConfig class? Where are you calling it? Your RouteDebugger output seems to be displaying a different set of routes than what you have defined. My guess is that you've got code on global.asax.cs that is registering routes (MVC <= 3 style), and your sample is in App_Start (MVC 4 style) but not being called. – Colin Young Apr 19 '13 at 13:52
  • Yeah! That works! Thank you very much!!! I'm using MVC 4. I have to map my routes in RoutConfig that is stored in App_Start folder. _Colin Young_, could you write your comment as full answer, so I can mark it as best answer? Thanks. – Алексей Стецюра Apr 19 '13 at 14:33

2 Answers2

1

In MVC 4, RouteConfig.cs is stored in App_Start. If you create your project from the MVC 4 project wizard it should wire it all up for you. If not, you will need to call it from global.asax.cs:

protected void Application_Start()
{
    // other stuff ...
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    // other stuff ...
}

Also, IIRC, the MVC 3 project wizard sets up all the default routes directly in Application_Start, so you'll need to remove those (or move them into RegisterRoutes as the case may be).

Colin Young
  • 3,018
  • 1
  • 22
  • 46
1

RoutConfig is read sequentially and "Default {controller}/{action}" is very greedy. Move your "Localization" route code above the "Default" route and see if that helps.