views:

328

answers:

2

UPDATE - 1/21/09:

The only way I've been able to get this working moderately well is to create routes that have additional path info... in other words instead of http://company.com/myDepartment/myTeam/action/id, the routes need to be built to handle paths like http://company.com/department/myDepartment/team/myTeam and so on.

END UPDATE - 1/21/09:

I have two possible URL conventions that I need to be able to route to the same controllers/views

http://team.department.company.com/Action/id

http://department.company.com/Action/id

http://company.com/Action/id

The above has been pretty easy to handle, since the department/team aliases are in the hostname of the Uri.

However, when I want to implement the following, I've been unable to get it working satisfactorily:

http://company.com/department/team/Action/id

http://company.com/department/Action/id

http://company.com/Action/id

Here are the routes I've defined:

routes.MapRoute("departmentAndTeam1", "{departmentId}/{teamId}/{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = "" });

routes.MapRoute("departmentAndTeam2", "{departmentId}/{teamId}/{controller}/{action}", new { controller = "Home", action = "Index" });

routes.MapRoute("departmentAndTeam3", "{departmentId}/{teamId}/{controller}", new { controller = "Home", action = "Index" });

routes.MapRoute("department", "{department}/{controller}/{action}/{id}", new {controller = "Home", action = "Index", id = ""});

routes.MapRoute("departmentAndTeam4", "{departmentId}/{teamId}/", new { controller = "Home", action = "Index"});

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

The problem I'm having is now every action method in my controllers require a departmentId & teamId parameter. I would really prefer just to be able to "pre-process" the department/team id values in the base controller class that I've got in the OnActionExecuting event handler. Am I barking up the wrong tree? Do I need to implement my own Mvc Route handler?

Note: This is replacing a legacy app and the URL formats need to be kept/maintained.

EDIT: First off, thanks for the responses so far. Based on the responses I've received I think I've not been clear enough on one point. This is (loosely) what my controller looks like:

public class ControllerBase : Controller
{
    DepartmentDTO department;
    TeamDTO team;
    protected override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        PopulateDept(filterContext.ActionParameters("departmentId");
        PopulateTeam(filterContext.ActionParameters("teamId");
    }
}

However, with this scheme, all controllers that inherit from this class (and are routed appropriately with the routes listed above) expect (what I perceive to be redundant/uneeded) parameters in their action handlers. I want to avoid having each handler require these "extra" parameters and am looking for suggestions.

Thanks! -Giorgio

+1  A: 

First off, you don't need departmentAndTeam2 or departmentAndTeam3. departmentAndTeam1 can handle it because it has the defaults for controller, action and id.

Another thing I notice is that none of your example URLs have the controller specified in them. In that case, leave it out of the route and just leave it as the default. eg

for the URL

http://company.com/department/team/Action/id

use the route

routes.MapRoute("departmentAndTeam1", "{departmentId}/{teamId}/{action}/{id}", new { controller = "Home", action = "Index", id = "" });

Notice how "controller" will always be "Home" because it's never mentioned in the URL.

For the actions and URLs that you don't want to specify the department and team IDs, they should just be passing by all the other routes and just using the Default route. The fact that they aren't means that they are getting caught by one of the earlier routes. I think if you fix the earlier route's problems then it should fix the problems of needing the extra IDs.

Finally, if you are still haveing trouble, try using an MVC route debugger like this one from Phil Haacked http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx. I've used it to work out the problems with some of my routes and it is really helpful to see what it is actually doing and runtime.

Jero
A: 

How about this (I haven't tested because I'm not sure what requirements you have other than those 3 particular urls mapping)?

routes.MapRoute("1", "{department}/{team}/{action}/{id}", new { controller = "Home" });
routes.MapRoute("2", "{department}/{action}/{id}", new { controller = "Home" });
routes.MapRoute("3", "{action}/{id}", new { controller = "Home" });
Kevin Pang