cdmckay
cdmckay

Reputation: 32240

ASP.NET MVC Custom Controller directory

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.

Upvotes: 1

Views: 3293

Answers (3)

cdmckay
cdmckay

Reputation: 32240

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

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

Upvotes: 4

eu-ge-ne
eu-ge-ne

Reputation: 28153

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]" }
);

Upvotes: 1

mxmissile
mxmissile

Reputation: 11673

I think what you are looking for is SubControllers.

Upvotes: 1

Related Questions