tags:

views:

41

answers:

2

Hi,

I want to remove the controller name from my URL (for one specific controller). For example:

http://mydomain.com/MyController/MyAction

I would want this URL to be changed to:

http://mydomain.com/MyAction

How would I go about doing this in MVC? I am using MVC2 if that helps me in anyway.

+4  A: 

You should map new route in the global.asax (add it before the default one), for example:

routes.MapRoute("SpecificRoute", "{action}/{id}", new {controller = "MyController", action = "Index", id = UrlParameter.Optional});

// default route
routes.MapRoute("Default", "{controller}/{action}/{id}", new {controller = "Home", action = "Index", id = UrlParameter.Optional} );
rrejc
A: 

You'll have to modify the default routes for MVC. There is a detailed explanation at ScottGu's blog: http://weblogs.asp.net/scottgu/archive/2007/12/03/asp-net-mvc-framework-part-2-url-routing.aspx

The method you should change is Application_Start. Something like the following might help:

RouteTable.Routes.Add(new Route(
  Url="MyAction"
  Defaults = { Controller = "MyController", action = "MyAction" },
  RouteHandler = typeof(MvcRouteHandler)
}

The ordering of the routes is significant. It will stop on the first match. Thus the default one should be the last.

sukru