views:

28

answers:

2

specifically, the default directory naming structure is [Controller]/[ActionMethod] resulting in a rendered url like www.mysite.com/home/actionMethodName.

What if I want to simply imply the Controller (in this example, 'home') so that I can get a url that looks like this: www.mysite.com/actionMethodName.

I haven't seen many requests for this kind of configuration. I can see how it breaks convention, but I would imagine that there are lots of people who need root pathing.

+2  A: 

Easy as apple pie - you just specify a route of your own! =)

An example:

routes.MapRoute(
    "RootPathing",
    "{action}",
    new { controller = "Default", action = "Index" });

This will register a route that catches all paths, and try to map them to the DefaultController with an action name corresponding to the path. However, note that if you place this route above the included default route, you will not be able to reach any other controller than the DefaultController - hence, place this route below the default route in the chain. It will then be matched by all paths that don't match a controller name. When debugging routes, Phil Haack's Routing Debugger is really worth taking a look at.

Tomas Lycken
I don't have time to confirm this now, but I'm quite sure that this will fail as the url will be matched by the default route (it matches pretty much everything) if you have that above this route. This will throw a exception saying that it can't find the controller. Instead you could add more specific routes before this one to make sure that they don't match the urls that this route should match.
Mattias Jakobsson
@Mattias: That might be true. I can't test it right now either, but the solution in that case is to add one specific route for each controller, with the pattern `"~/Home/{action}"` for the `HomeController`, etc, so it's easily fixed. And it's a problem you can't really avoid - if you want to be able to choose an action on the route path, you can't be too generic on any routes; that's a limitation of the concept of routing, not of the MVC Framework.
Tomas Lycken
@Tomas Lycken, Yes, of course. You could also implement your own routing class and check if the url is a valid action or not and then add that first in the routing list. But in this case its probably better to write more specific routes before.
Mattias Jakobsson
yeah, adding the initial solution to the Global.config yields this error: "The route URL cannot start with a '/' or '~' character and it cannot contain a '?' character.Parameter name: routeUrl"
Neurothustra
@Neurothustra, Just remove ~/ from the route and it should work.
Mattias Jakobsson
@Neuro: Sorry about that - I've edited my post.
Tomas Lycken
A: 

Because you are planning to remove the {controller} element of the url, you may need to get a bit more specific with your other urls, e.g.:

routes.MapRoute("MyOtherControllerRoute", "Account/{action}", new { controller = "Account", action = "Index" });
routes.MapRoute("MyDefaultRoute", "{action}", new { controller = "Home", action = "Index" });

When the route table is interrogated, if the url such as www.mysite.com/Account is used, it will match the first route, because we have been specific about the pattern used to match the url. If we then do something like www.mysite.com/DoSomething it will use the default route we've selected last, trying to invoke the DoSomething action on the HomeController type.

Something which I've noticed is that a lot of MVC developers seems to assume that the url is strictly {something}/{something}/{something}, whereas it can essentially be anything you like, e.g, I can have a route that does: www.mysite.com/my-weird-and-wonderful-url which I could map specifically:

routes.MapRoute("Somewhere", "my-weird-and-wonderful-url", new { controller = "Whatever", action = "Whenever" });

Hope that helps.

Matthew Abbott
Thanks Matthew - that worked just fine. Thanks to everyone who offered their insight - I'm glad a solution was quickly made available.
Neurothustra