1

是否可以为 ASP.NET MVC 定义更多文件夹来搜索 Views 或 Partials?

例如,如果我浏览到 /Home/Index 并且 Index 操作返回 View(),ASP.NET MVC 将查看以下位置:

  • ~/Views/Home/Index.aspx
  • ~/Views/Home/Index.ascx
  • ~/Views/Shared/Index.aspx
  • ~/Views/Shared/Index.ascx
  • ~/Views/Home/Index.cshtml
  • ~/Views/Home/Index.vbhtml
  • ~/Views/Shared/Index.cshtml
  • ~/Views/Shared/Index.vbhtml

我想创建另一个文件夹,比如 ~/Views/PartivalViews/,它将被搜索。

显然,我正在寻找这种方式来存储我的 PartialViews。

4

2 回答 2

5

您可以编写一个custom view engine可以指定 ASP.NET MVC 将在其中查找视图的其他文件夹的位置。

这里的想法是编写一个派生自RazorViewEngine并在其构造函数中设置各种属性的类,例如:

  • 区域视图位置格式
  • AreaMasterLocationFormats
  • AreaPartialViewLocationFormats
  • 查看位置格式
  • MasterLocation格式
  • PartialViewLocationFormats

以下是您可以随意覆盖的默认值:

public RazorViewEngine(IViewPageActivator viewPageActivator) : base(viewPageActivator)
{
    base.AreaViewLocationFormats = new string[] { "~/Areas/{2}/Views/{1}/{0}.cshtml", "~/Areas/{2}/Views/{1}/{0}.vbhtml", "~/Areas/{2}/Views/Shared/{0}.cshtml", "~/Areas/{2}/Views/Shared/{0}.vbhtml" };
    base.AreaMasterLocationFormats = new string[] { "~/Areas/{2}/Views/{1}/{0}.cshtml", "~/Areas/{2}/Views/{1}/{0}.vbhtml", "~/Areas/{2}/Views/Shared/{0}.cshtml", "~/Areas/{2}/Views/Shared/{0}.vbhtml" };
    base.AreaPartialViewLocationFormats = new string[] { "~/Areas/{2}/Views/{1}/{0}.cshtml", "~/Areas/{2}/Views/{1}/{0}.vbhtml", "~/Areas/{2}/Views/Shared/{0}.cshtml", "~/Areas/{2}/Views/Shared/{0}.vbhtml" };
    base.ViewLocationFormats = new string[] { "~/Views/{1}/{0}.cshtml", "~/Views/{1}/{0}.vbhtml", "~/Views/Shared/{0}.cshtml", "~/Views/Shared/{0}.vbhtml" };
    base.MasterLocationFormats = new string[] { "~/Views/{1}/{0}.cshtml", "~/Views/{1}/{0}.vbhtml", "~/Views/Shared/{0}.cshtml", "~/Views/Shared/{0}.vbhtml" };
    base.PartialViewLocationFormats = new string[] { "~/Views/{1}/{0}.cshtml", "~/Views/{1}/{0}.vbhtml", "~/Views/Shared/{0}.cshtml", "~/Views/Shared/{0}.vbhtml" };
    base.FileExtensions = new string[] { "cshtml", "vbhtml" };
}

然后只需在以下位置注册您的自定义视图引擎Application_Start

ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new MyRazorViewEngine());

在此示例中,我在注册自定义视图引擎之前删除了所有其他默认视图引擎(WebForms 和 Razor)。

于 2013-02-01T11:42:22.200 回答
0

如果您想知道如何从上面的 Darin 回答开始,这就是我实现它的方式。所有功劳都归功于达林和欧文。

我基本上想把我所有的部分视图放在Views/Controller/Shared文件夹下。所以我只替换了“RazorViewEngine”的“PartialViewLocationFormats”属性。添加“ ~/Views/{1}/Shared/{0}.cshtml”作为列表中的第一个元素,以便 ViewEngine 将"Views/Controller/Shared首先查看“”文件夹。

然后正如 Darin 在上面的 global.asax 中解释的那样,清除现有的视图引擎并添加新的。

ViewEngines.Engines.Add(new CustomRazorViewEngine());

希望这可以帮助某人。

public class CustomRazorViewEngine : RazorViewEngine
    {
        public CustomRazorViewEngine()
        {
            var newLocationFormat = new[]
                                    {
                                        "~/Views/{1}/Shared/{0}.cshtml",
                                        "~/Views/{1}/{0}.cshtml", 
                                        "~/Views/{1}/{0}.vbhtml", 
                                        "~/Views/Shared/{0}.cshtml", 
                                        "~/Views/Shared/{0}.vbhtml"
                                    };

            PartialViewLocationFormats = newLocationFormat;
        }

    }
于 2014-02-18T04:55:39.167 回答