asp.net-mvc - 将 MVC3 View 分组到主 'action' 文件夹下的子文件夹中

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

例如。我有三个支付 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/

相关文章:

c# - 命名 View "Properties"

c# - 转发到 Controller 的动态 DateTime 控件数量

c# - 使用 iTextSharp 链接到 PDF

asp.net-mvc-3 - MVC4 Razor 中可能存在重大变化,可通过 "@:@"修复

c# - 不同 View 中的相同字段名称无法正确呈现

c# - 使用 ASP.net 将 Highcharts 连接到数据库

asp.net-mvc - ASP.NET MVC : What is the purpose of @section?

asp.net-mvc - 如何使用 Ninject 将 ModelState 作为参数注入(inject)?

c# - 显示验证成功的消息

c# - ASP.NET 网页 (Razor) 中的验证已弃用?