tags:

views:

31

answers:

2

i have a ApplicationController class with an action called Admin

so my url is www.mysite.com/Application/Admin

is there anyway i can have the routing be www.mysite.com/Admin and go to this method.

i know i can do this by creating an AdminController but for this one function i thought it was easier to just put in another controller

+1  A: 

You can set the Application controller and the Admin method as the default controller and action, using parameter defaults:

routes.MapRoute(
    "Default", // Route name
    "{action}", // URL with parameters
    new { controller = "Application", action = "Admin" }
);

If this is your last route, it will match any request that does not have a controller name and an action name in it. In this particular example, even a request without an action will execute your Admin action, since it's the default action.

Note that routes with parameter defaults can create strange behavior in your existing routes, if you have any. You can always use the ASP.NET MVC Routing Debugger to test which routes match a given URL.

bzlm
You still need a new route.
Robert Harvey
@Robert This *is* the new route. :)
bzlm
I don't think this one will match mysite.com/action.
Robert Harvey
@Robert You're right. Now it will. :)
bzlm
+1 for the route debugger. Everyone who programs in ASP.NET MVC should know about it, and the [NerdDinner tutorial.](http://nerddinnerbook.s3.amazonaws.com/Intro.htm)
Robert Harvey
+3  A: 

Put this above your default route:

routes.MapRoute(
    "ShortRoute",
    "{action}",
    new { controller = "Application", action = "Index"}
);
Robert Harvey
+1, good answer @Robert.
Darin Dimitrov