Using Url.RouteUrl() with Route Names in an Area

Everyone please see @ryanulit's answer below. This issue may be fixed for you with a newer framework version.

Not sure if there has been a hot fix, but the behavior now is a bit different. Using your exact code and trying:

Url.RouteUrl("UserHome", new { id = 5 })

I now get:

/User/5?httproute=True 

This still looks awkward, so I experimented with the route and added another default param:

 context.MapRoute(
            "UserHome",
            "User/{id}",
            new { action = "Index", controller = "Home", area = "User", id = 0, 
                       httproute = true },
            new { controller = @"Home", id = @"\d+" }
        );

Now when I use

Url.RouteUrl("UserHome", new { id = 5 })

I get a nice url of

/User/5

disclaimer There could be unwanted side effects of httproute=true in the route declaration.

Also, the more verbose use:

@Url.RouteUrl("UserHome", new { controller = "Home", action = "Index", id = 5 })

still works as well.


Try this:

@Url.Action("Index", "Home", new { Area = "User" })