leora
leora

Reputation: 196429

URL routing in asp.net mvc

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

Upvotes: 0

Views: 102

Answers (2)

bzlm
bzlm

Reputation: 9727

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.

Upvotes: 1

Robert Harvey
Robert Harvey

Reputation: 180787

Put this above your default route:

routes.MapRoute(
    "ShortRoute",
    "{action}",
    new { controller = "Application", action = "Index"}
);

Upvotes: 3

Related Questions