在ASP.NET MVC中定义更多“共享”文件夹

时间:2013-02-01 11:32:10

标签: asp.net-mvc asp.net-mvc-4 partial-views

是否可以为ASP.NET MVC定义更多文件夹以搜索视图或部分?

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

  • 〜/查看/主页/的Index.aspx
  • 〜/查看/主页/ Index.ascx
  • 〜/查看/共享/ Index.aspx的
  • 〜/查看/共享/ Index.ascx
  • 〜/查看/主页/ Index.cshtml
  • 〜/查看/主页/ Index.vbhtml
  • 〜/查看/共享/ Index.cshtml
  • 〜/查看/共享/ Index.vbhtml

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

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

2 个答案:

答案 0 :(得分:5)

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

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

  • AreaViewLocationFormats
  • AreaMasterLocationFormats
  • AreaPartialViewLocationFormats
  • ViewLocationFormats
  • MasterLocationFormats
  • 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)。

答案 1 :(得分:1)

如果您想知道如何从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;
        }

    }