views:

159

answers:

2

I have - I think - a complex URL to deal with in ASP MVC 1.0: All my actions in most of the controllers require two parameters all the time: Account and Project. This is on top of each Action's requirements. This means a typical URL is like this:

http://abcd.com/myaccount/projects/project_id/sites/edit/12

In this example: myaccount is the account name. projects can be a controller, others options are like locations, employees. project_id is the id of a project within myaccount, sites could be a controller, other options are like staff or payments. edit is an action and 12 is the id of the site edited. (hope this is clear enough)

Now one option is to create a route and pass project_id and account into all actions of controllers by adding two extra parameters to all actions. This is not really desired and also I'm not sure the two controllers (projects and sites) are going to work here.

My ideal situation is to use some kind of context that travels with the call to the controller action and store project_id and myaccount in there. The rest of the parameters then can be dealt with in a normal way like:

// sitescontroller
public ActionResult Edit(string id)
{
string account = somecontext["account"];
string project_id = somecontext["project"];
// do stuff
}

Any ideas as to how/where this can happen? Also how is this going to work with ActionLink (i.e. generating correct links based on this context)?

Thanks!

A: 

This doesn't quite make sense to me. Why would you have a route that is akin to the following:

{controller}/{id}/{controller}/{id}

?

Dan Atkinson
This is one way of looking at it. This is exactly the same as the way Basecamp (37Signals) works. If you create an account in Basecamp, you get a url like this:http://accountName.basecamphq.com/projects/123/notes/345ZenDesk is also exactly the same. (I think so is GetSatisfaction). My requirements are very similar.
Khash
But surely, the accountname isn't a controller, but a parameter that isn't needed by the controller, but the application itself to ensure that your pages go to the correct company.You could do something as John Downey suggested. We've done something similar where I am, overriding the controllers with a parent controller, and capturing what is effectively the accountname param, and displaying/authenticating differently depending on who they are, and where they want to go.
Dan Atkinson
+1  A: 

You first need to add the tokens to your routes like {company}/projects/{project}{controller}/{action}/{id}. Then if you wrote your own IControllerFactory then it would be very easy to push the values from the RouteData into the controller via the constructor or however you wanted to do it. Probably the easiest way to get started would be to subclass DefaultControllerFactory and override the CreateController method.

John Downey