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

后端 未结 10 2332
故里飘歌
故里飘歌 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: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

提交回复
热议问题