【问题标题】:Defining more "Shared" folders in ASP.NET MVC在 ASP.NET MVC 中定义更多“共享”文件夹
【发布时间】:2013-01-16 17:03:51
【问题描述】:

是否可以为 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 的整洁方式。

【问题讨论】:

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


【解决方案1】:

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

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

  • AreaViewLocationFormats
  • AreaMasterLocationFormats
  • AreaPartialViewLocationFormats
  • 查看位置格式
  • 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)。

【讨论】:

【解决方案2】:

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

    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-15
    • 1970-01-01
    • 1970-01-01
    • 2016-05-24
    相关资源
    最近更新 更多