例如。我有三个支付 Controller ,每个都特定于第三方支付处理器,所以在我的根 View 文件夹下,我为每个 Controller 都有一个文件夹。我想将这些移动到 Views\Payments\Processor1、Views\Payments\Processor2 等中,而不是当前的 Views\Processor1 等。
我还没有准备好实现区域,所以我希望有一些方法可以告诉 MVC 也查看子文件夹或类似的东西。这可以做到吗?如何做到?
最佳答案
您可以编写自定义 View 引擎和 override the default view locations :
public class MyRazorViewEngine : RazorViewEngine
{
public MyRazorViewEngine() : base()
{
base.ViewLocationFormats = base.ViewLocationFormats.Concat(new[] {
"~/Views/Payments/{1}/{0}.cshtml",
"~/Views/Payments/{1}/{0}.vbhtml"
}).ToArray();
base.PartialViewLocationFormats = base.PartialViewLocationFormats.Concat(new[] {
"~/Views/Payments/{1}/{0}.cshtml",
"~/Views/Payments/{1}/{0}.vbhtml"
}).ToArray();
}
}
然后在
Application_Start
注册:ViewEngines.Engines.Add(new MyRazorViewEngine());
关于asp.net-mvc - 将 MVC3 View 分组到主 'action' 文件夹下的子文件夹中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6840941/