Handling Complex URLs in ASP MVC
I have - I think - a complex URL to work in ASP MVC 1.0: All my actions in most controllers require two parameters: Account and Project. This fits the requirements of every Action. This means that a typical url looks like this:
http://abcd.com/myaccount/projects/project_id/sites/edit/12
In this example: myaccount is the account name. projects can be controller, other parameters are similar to locations , employees . project_id is the id of the project in myaccount , sites , which can be a controller, other parameters are like personnel or payments . edit is the action, and 12 is the ID of the site being edited. (hope this is clear enough)
Now one option is to create a route and pass the project_id and account to all controller actions, adding two additional parameters to all actions. This is not very desirable and I am not sure if two controllers (projects and sites) will work here.
My ideal situation is to use some kind of context that travels with the call to the controller action and stores the project_id and myaccount there. The rest of the parameters can then be processed in the usual way, for example:
// sitescontroller
public ActionResult Edit(string id)
{
string account = somecontext["account"];
string project_id = somecontext["project"];
// do stuff
}
Any ideas as to how / where this might happen? Also how will this work with ActionLink (i.e. generating correct links based on this context)?
Thanks!
a source to share
First you need to add markers to your routes eg {company}/projects/{project}{controller}/{action}/{id}
. Then, if you wrote your own IControllerFactory, it would be very easy to push the values from RouteData into the controller via the constructor, or still you wanted to do it. Probably the easiest way to start is by subclassing the DefaultControllerFactory and overriding the CreateController method.
a source to share