简体   繁体   English

MVC嵌套控制器和视图

[英]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. 我尝试了一些变化,但无法通过MVC推断来识别我的项目结构。

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 . 即,使用简单return PartialView(piston) EditPiston动作return PartialView(piston)想要使用视图Views-> Engines -> EditPiston.ascx ,而不是Views-> Engines -> EditPiston.ascx Views-> Cars->Engines -> EditPiston.ascx

Also, what do I need to do in the RegisterArea? 另外,我在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. 另一种选择是创建自己的从RazorViewEngine派生的View Engine,它可以添加更多搜索路径,但是您应该知道这些路径将搜索每个页面渲染,如果您有很多搜索路径,可能会显着降低性能。

An example of creating your own ViewEngine is here: 创建自己的ViewEngine的示例如下:

Can I specify a custom location to "search for views" in ASP.NET MVC? 我可以在ASP.NET MVC中指定“搜索视图”的自定义位置吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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