Can I specify a custom location to “search for views” in ASP.NET MVC?

后端 未结 10 2208
故里飘歌
故里飘歌 2020-11-22 06:08

I have the following layout for my mvc project:

  • /Controllers
    • /Demo
    • /Demo/DemoArea1Controller
    • /Demo/DemoArea2Controller
    • etc
相关标签:
10条回答
  • 2020-11-22 06:36

    Most of the answers here, clear the existing locations by calling ViewEngines.Engines.Clear() and then add them back in again... there is no need to do this.

    We can simply add the new locations to the existing ones, as shown below:

    // note that the base class is RazorViewEngine, NOT WebFormViewEngine
    public class ExpandedViewEngine : RazorViewEngine
    {
        public ExpandedViewEngine()
        {
            var customViewSubfolders = new[] 
            {
                // {1} is conroller name, {0} is action name
                "~/Areas/AreaName/Views/Subfolder1/{1}/{0}.cshtml",
                "~/Areas/AreaName/Views/Subfolder1/Shared/{0}.cshtml"
            };
    
            var customPartialViewSubfolders = new[] 
            {
                "~/Areas/MyAreaName/Views/Subfolder1/{1}/Partials/{0}.cshtml",
                "~/Areas/MyAreaName/Views/Subfolder1/Shared/Partials/{0}.cshtml"
            };
    
            ViewLocationFormats = ViewLocationFormats.Union(customViewSubfolders).ToArray();
            PartialViewLocationFormats = PartialViewLocationFormats.Union(customPartialViewSubfolders).ToArray();
    
            // use the following if you want to extend the master locations
            // MasterLocationFormats = MasterLocationFormats.Union(new[] { "new master location" }).ToArray();   
        }
    }
    

    Now you can configure your project to use the above RazorViewEngine in Global.asax:

    protected void Application_Start()
    {
        ViewEngines.Engines.Add(new ExpandedViewEngine());
        // more configurations
    }
    

    See this tutoral for more info.

    0 讨论(0)
  • 2020-11-22 06:40

    There's actually a lot easier method than hardcoding the paths into your constructor. Below is an example of extending the Razor engine to add new paths. One thing I'm not entirely sure about is whether the paths you add here will be cached:

    public class ExtendedRazorViewEngine : RazorViewEngine
    {
        public void AddViewLocationFormat(string paths)
        {
            List<string> existingPaths = new List<string>(ViewLocationFormats);
            existingPaths.Add(paths);
    
            ViewLocationFormats = existingPaths.ToArray();
        }
    
        public void AddPartialViewLocationFormat(string paths)
        {
            List<string> existingPaths = new List<string>(PartialViewLocationFormats);
            existingPaths.Add(paths);
    
            PartialViewLocationFormats = existingPaths.ToArray();
        }
    }
    

    And your Global.asax.cs

    protected void Application_Start()
    {
        ViewEngines.Engines.Clear();
    
        ExtendedRazorViewEngine engine = new ExtendedRazorViewEngine();
        engine.AddViewLocationFormat("~/MyThemes/{1}/{0}.cshtml");
        engine.AddViewLocationFormat("~/MyThemes/{1}/{0}.vbhtml");
    
        // Add a shared location too, as the lines above are controller specific
        engine.AddPartialViewLocationFormat("~/MyThemes/{0}.cshtml");
        engine.AddPartialViewLocationFormat("~/MyThemes/{0}.vbhtml");
    
        ViewEngines.Engines.Add(engine);
    
        AreaRegistration.RegisterAllAreas();
        RegisterRoutes(RouteTable.Routes);
    }
    

    One thing to note: your custom location will need the ViewStart.cshtml file in its root.

    0 讨论(0)
  • 2020-11-22 06:40

    If you want just add new paths, you can add to the default view engines and spare some lines of code:

    ViewEngines.Engines.Clear();
    var razorEngine = new RazorViewEngine();
    razorEngine.MasterLocationFormats = razorEngine.MasterLocationFormats
          .Concat(new[] { 
              "~/custom/path/{0}.cshtml" 
          }).ToArray();
    
    razorEngine.PartialViewLocationFormats = razorEngine.PartialViewLocationFormats
          .Concat(new[] { 
              "~/custom/path/{1}/{0}.cshtml",   // {1} = controller name
              "~/custom/path/Shared/{0}.cshtml" 
          }).ToArray();
    
    ViewEngines.Engines.Add(razorEngine);
    

    The same applies to WebFormEngine

    0 讨论(0)
  • 2020-11-22 06:41

    You can easily extend the WebFormViewEngine to specify all the locations you want to look in:

    public class CustomViewEngine : WebFormViewEngine
    {
        public CustomViewEngine()
        {
            var viewLocations =  new[] {  
                "~/Views/{1}/{0}.aspx",  
                "~/Views/{1}/{0}.ascx",  
                "~/Views/Shared/{0}.aspx",  
                "~/Views/Shared/{0}.ascx",  
                "~/AnotherPath/Views/{0}.ascx"
                // etc
            };
    
            this.PartialViewLocationFormats = viewLocations;
            this.ViewLocationFormats = viewLocations;
        }
    }
    

    Make sure you remember to register the view engine by modifying the Application_Start method in your Global.asax.cs

    protected void Application_Start()
    {
        ViewEngines.Engines.Clear();
        ViewEngines.Engines.Add(new CustomViewEngine());
    }
    
    0 讨论(0)
提交回复
热议问题