简体   繁体   中英

MVC Nested Controllers and Views

Some of our controllers are getting too large, so I am trying to simplify them by refactoring along the lines of:

Areas
    Vehicles
        Controllers
            Cars
                CarController.cs
                Engines
                    PistonController.cs
                    ...
        Models
        Views
            Cars
                EditCar.ascx
                Engines
                    EditPiston.ascx
                    ShowPiston.ascx
    Animals
    ...

(with namespaces matching the folder structure)

I've tried a few variations but can't get the MVC inference to recognise my project structure.

What does the partial view return look like in the action? Ie, EditPiston action with a simple return PartialView(piston) wants to use a view Views-> Engines -> EditPiston.ascx , not Views-> Cars->Engines -> EditPiston.ascx .

Also, what do I need to do in the RegisterArea? Any specific routes I need to set up?

Out of the box, you can't do this using standard conventions.

You can, of course, simply specify the views locations directly. ie:

return View("~/Areas/Views/Vehicles/Cars/Engines/Piston.cshtml");

Another option would be to create your own View Engine derived from RazorViewEngine which adds more search paths, however you should be aware that these paths will be search for each page render, and can significantly decrease performance if you have a lot of search paths.

An example of creating your own ViewEngine is here:

Can I specify a custom location to "search for views" in ASP.NET MVC?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM