E.g。我有三个支付控制器,每个支持控制器特定于第三方支付处理器,因此在我的根视图文件夹下,每个控制器都有一个文件夹。我想将它们移到Views \ Payments \ Processor1,Views \ Payments \ Processor2等中,而不是当前的Views \ Processor1等。
我还没有准备好实现区域,所以我希望有一些方法可以告诉MVC也要查看子文件夹,或类似的东西。可以这样做吗?
答案 0 :(得分:11)
您可以编写自定义视图引擎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());
答案 1 :(得分:2)
您是否需要搜索视图?您可以在View()调用中指定要使用的视图,并使用路径。