views:

328

answers:

3

Hello,

I'm making an ASP.NET MVC website and I have a certain group of forms that are related (let's called them Foo). My first attempt to organized them was to have this structure:

Controllers/FooController.cs

...and to have routes like this:

Foo/{type}/{action}/{id}

Unfortunately, since there are about 8 Foo sub-types, the FooController was getting quite large and containing it's own sub-routing information. My next stab at it was to have this structure:

Controllers/Foo/Form1Controller.cs
Controllers/Foo/Form2Controller.cs
Controllers/Foo/Form3Controller.cs
...

With a controller for each form, which makes more sense to me since that's basically the layout I use for other forms in the app.

Unfortunately, I can't seem to find any easy way to make the route:

Foo/{controller}/{action}/{id}

...map to:

Controllers/Foo/{controller}Controller.cs

Basically what I want to do is tell ASP.NET MVC that I want all routes that match the Foo route to look in the Foo subfolder of Controllers for their controllers. Is there any easy way to do that via routing, or do I need to write my own IControllerFactory?

Thanks.

+1  A: 

I think what you are looking for is SubControllers.

mxmissile
+1  A: 

Did you try RouteConstraint? It is not about directories rather controller names but very simple - you should try:

routes.MapRoute("FooControllers",
    "{controller}/{action}/{id}",
    new { controller = "Home", action = "Index", id = "" },
    new { controller = @"(Form1)|(Form2)|(Form3)" }
);

or:

routes.MapRoute("FooControllers",
    "{controller}/{action}/{id}",
    new { controller = "Home", action = "Index", id = "" },
    new { controller = @"Form[1..8]" }
);
eu-ge-ne
This still won't let me have my Foo forms in a subfolder under Controllers.
cdmckay
+4  A: 

I ended up using a variation of the solution discussed here:

http://haacked.com/archive/2008/11/04/areas-in-aspnetmvc.aspx

cdmckay
Good simple find. Supposed to be built in the next version of MVC.
mxmissile
Yeah, I was pretty surprised that it was this complicated. However, I guess it is only at 1.0.
cdmckay