asp.net-mvc 在 ASP.NET MVC 3 的子文件夹中添加视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13062235/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add views in sub-folders in ASP.NET MVC 3
提问by Kailas Mane
I am working on ASP.NET MVC 3 project. I want to divide controllers, models, and views in sub-folders for the sake of simplicity. I able to do it with controllers and models but when i create a view it creates automatically to root folder Views, Is there any way to solve this problem?
我正在研究 ASP.NET MVC 3 项目。为了简单起见,我想在子文件夹中划分控制器、模型和视图。我可以用控制器和模型来做,但是当我创建一个视图时,它会自动创建到根文件夹Views,有什么办法可以解决这个问题?
eg. I can create
例如。我可以创造
model class as,
模型类,
Models/Finance/Bank.cs
Models/Finance/Finance.cs
Models/Production/Production.cs
controller as,
控制器作为,
Controllers/Finance/BankController/Create
Controllers/Finance/BudgetController/Create
Controllers/Production/ProcessController/Create
but where i tried to create view for above actions, it creates in to,
但是在我尝试为上述操作创建视图的地方,它创建了,
Views/Bank/Create.aspx
Views/Budget/Create.aspx
Views/Process/Create.aspx
I want it should be like,
我希望它应该像,
Views/Finance/Bank/Create.aspx
Views/Finance/Budget/Create.aspx
Views/Prodution/Process/Create.aspx
Is there any way to create views in same sub-folder as of that created for Controllers and models? thanks!
有没有办法在与为控制器和模型创建的子文件夹相同的子文件夹中创建视图?谢谢!
回答by Kailas Mane
following steps worked for me,
以下步骤对我有用,
Create sub-folders as you want in
Views(root folder). in my case it was Finance & Production.Simply drag automatically created folders in
Viewsin to appropriate Sub-folders. in my caseBank&Budgetin toFinanceandProcessin toProductionWhile you return a view from controller action, give full path of view as,
returnView("~/Views/Finance/Bank/Create.aspx")returnView("~/Views/Finance/Budget/Create.aspx")returnView("~/Views/Production/Process/Create.aspx")
根据需要在
Views(根文件夹)中创建子文件夹。就我而言,它是财务与生产。只需将自动创建的文件夹拖入
Views适当的子文件夹即可。在我的情况下Bank&Budgetin toFinance和Processin toProduction当你从控制器动作返回一个视图时,给出完整的视图路径,
returnView("~/Views/Finance/Bank/Create.aspx")returnView("~/Views/Finance/Budget/Create.aspx")returnView("~/Views/Production/Process/Create.aspx")
回答by Erik Funkenbusch
Models and Controllers are compiled source files. They get compiled into a DLL, and as such, they can literally be put anywhere in the project and it won't make a difference. These classes are have no concept of their location in the filesystem because they don't exist in the filesystem once compiled.
模型和控制器是编译好的源文件。它们被编译成一个 DLL,因此,它们实际上可以放在项目中的任何地方,不会有什么不同。这些类没有它们在文件系统中的位置的概念,因为它们在编译后不存在于文件系统中。
Views, on the other hand are different. They are text files that are deployed to the server and loaded and parsed at run-time, thus the framework has to know where to find them.
另一方面,观点不同。它们是部署到服务器并在运行时加载和解析的文本文件,因此框架必须知道在哪里可以找到它们。
The tooling will always create the views in the ~\Views\Controller folder (or ~Areas\AreaName\Controller folder). You can move them anywhere you want after that, but you will have to give the entire folder path to the View() method (including .cshtml). Or you will have to implement a custom ViewEngine that sets the search paths where you want them.
该工具将始终在 ~\Views\Controller 文件夹(或 ~Areas\AreaName\Controller 文件夹)中创建视图。之后您可以将它们移动到您想要的任何位置,但您必须将整个文件夹路径提供给 View() 方法(包括 .cshtml)。或者您必须实现一个自定义 ViewEngine 来设置您想要的搜索路径。
回答by Jeroen
For future visitors: use areas.
对于未来的访客:使用区域。
Walkthrough: Organizing an ASP.NET MVC Application using Areas
回答by ChunHao Tang
View's named is According to name of Controller, you should follow the rule.
If you wanna it create Views/Admin/Create, then your CustomerController.csshould be named AdminController.cs.
视图的命名是根据控制器的名称,您应该遵循规则。
如果你想要它创建Views/Admin/Create,那么你CustomerController.cs应该被命名AdminController.cs。

