0

I'm wondering whether it's possible to have several nested controllers within one main one in MVC 3? For example:

public class AdminController : Controller
{
    public class PagesController : Controller
    {
    }
    //More controllers
}

I've tried this but couldn't get it to work, have modified my routes in global.asx but still nothing. How can I call the correct controller from AdminController when the url is for example:

/Admin/Pages/Index

4
  • 1
    What would be the point of this? What are you trying to do? Commented Aug 6, 2012 at 12:15
  • 1
    Look into routing: asp.net/mvc/tutorials/older-versions/controllers-and-routing/… Commented Aug 6, 2012 at 12:15
  • Why do you need to nest these controllers? Your could use your example URL without nesting if you set your route config up correctly. Commented Aug 6, 2012 at 12:17
  • @JustinHarvey I'm trying to consolidate a bunch of controllers (page/floor/availability) that have basic CRUD action methods, into one main admin one, currently they are separate and work fine. So i need my route to look in the admin controller, then select the correct controller by the second part of the url (pages for example) and then the correct action method within that class, if that makes sense? Commented Aug 6, 2012 at 12:40

2 Answers 2

7

It sounds like what you are after is Areas. That will give you the URL routing structure you are seeking, but it does not use "controllers within controllers" to accomplish this.

Instead you will have an Admin area with a Pages controller in that area.

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

Comments

0

No this is not allowed. The controller factory won't be able to resolve it.

Comments

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.