首页 文章

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

提问于
浏览
1

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

例如,如果我浏览到/ 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的整洁方式 .

2 回答

  • 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

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

相关问题