How to change View & partial view default location

后端 未结 2 1249
盖世英雄少女心
盖世英雄少女心 2020-12-30 14:41

i am new in MVC and very curious about to know that how could i change view & partial view location.

we know that view & partial view store in view folder. i

2条回答
  •  长发绾君心
    2020-12-30 15:13

    You can modify RazorViewEngine's ViewLocationFormats and PartialViewLocationFormats properties in your Global.asax startup code. Something around the lines below should work:

    protected void Application_Start(object obj, EventArgs e)
    {
       var engine = ViewEngines.Engines.OfType().Single();
       var newViewLocations = new string[] { 
           "~/SomeOtherFolder/{1}/{0}.cshtml", 
           "~/GlobalFolder/{0}.cshtml"      
       };
       engine.ViewLocationFormats = newViewLocations;
       engine.PartialViewLocationFormats = newViewLocations;
    }
    

    IIRC, {1} would correspond to controller and {0} to view name, you can look at existing properties to make sure.

    If you want to keep existing search locations you need to copy them into your new array.

提交回复
热议问题