This project is read-only.

Default route with baseRoute

Dec 30, 2011 at 12:08 PM

It's strange trouble with trying to define route "" with routes.MapRoute when using MCR and baseRoute:

            routes.MapCodeRoutes("{year}/{quarter}", typeof (HomeController),
                                 new CodeRoutingSettings
                                         RouteFormatter = RouteFormatter
            routes.MapRoute("Default", "", new { controller = "Redirect", action = "Index" });

I want to redirect from base application URL to the actual (setting in DB) quarter, '' -> ''. But, I can't build url using MVC classes in any combinations - it's always return null. It seems to CodeRoute is very special route, and classic Url.Action("Index", "Home", new { y = 2011, q = 3}) doesn't work...

Can you help me?

Dec 30, 2011 at 4:15 PM
You have to do this:

Url.Action("Index", "Home", new { y = 2011, q = 3, __mvccoderouting =
"{year}/{quarter}" })

Actually, that's a terrible name, so in the latest revision I've
changed it to this:

Url.Action("Index", "Home", new { y = 2011, q = 3, __routecontext =
"{year}/{quarter}" })

Another thing you have to be careful with, if the current route is not
CodeRoute then the special controller reference syntax won't work, so
instead of this:

Url.Action("", "~SomeController")

.. you have to do this:

Url.Action("", "SomeController", new { __routecontext = "baseRoute" })
Jan 10, 2012 at 9:43 AM

__routecontext helps, thanks!

Aug 6, 2013 at 9:20 AM
The code below appends __routecontext in the query string, is this normal?
Url.Action("", "SomeController", new { __routecontext = "baseRoute" })
Aug 6, 2013 at 1:52 PM
No, probably matching a route not created by MvcCodeRouting, is that the case?
Aug 6, 2013 at 2:56 PM
All my routes were created by MvcCodeRouting.

My project is MVC4 targeting .Net Framework 4.5

No one else is having the problem, I might be missed something.
Aug 6, 2013 at 3:18 PM
So, the generated URL is correct except __routecontext is in the querystring?
If all routes are created by MvcCodeRouting, why are you using __routecontext? It's a workaround for special cases. It still should work, so maybe there is an issue.
Could you please try to reproduce by changing the target to .NET 4.0