1

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.

2
  • Shouldnt Controllers/Foo/Form1Controller.aspx be Controllers/Foo/Form1Controller.cs? Unless I'm missing something. Commented Jul 14, 2009 at 20:22
  • Right right, but the question is still the same. I'll fix now. Commented Jul 14, 2009 at 20:26

3 Answers 3

4

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

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

Sign up to request clarification or add additional context in comments.

3 Comments

Good simple find. Supposed to be built in the next version of MVC.
Yeah, I was pretty surprised that it was this complicated. However, I guess it is only at 1.0.
@cdmckay Not that I think Haacked's site would ever go down or disappear but could you summarize on Stackoverflow to keep content consistent?
1

I think what you are looking for is SubControllers.

Comments

1

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

1 Comment

This still won't let me have my Foo forms in a subfolder under Controllers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.