将MVC3视图分组到主“action”文件夹下的子文件夹中

时间:2011-07-27 07:36:44

标签: asp.net-mvc asp.net-mvc-3 razor

E.g。我有三个支付控制器,每个支持控制器特定于第三方支付处理器,因此在我的根视图文件夹下,每个控制器都有一个文件夹。我想将它们移到Views \ Payments \ Processor1,Views \ Payments \ Processor2等中,而不是当前的Views \ Processor1等。

我还没有准备好实现区域,所以我希望有一些方法可以告诉MVC也要查看子文件夹,或类似的东西。可以这样做吗?

2 个答案:

答案 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()调用中指定要使用的视图,并使用路径。