ASP.NET Core MVC通過IViewLocationExpander擴充套件檢視搜尋路徑的實現
阿新 • • 發佈:2020-04-08
IViewLocationExpander API
- ExpandViewLocations Razor檢視路徑,檢視引擎會搜尋該路徑.
- PopulateValues 每次呼叫都會填充路由
專案目錄如下所示
建立區域擴充套件器,其實我並不需要多區域,我目前只需要達到一個區域中有多個資料夾進行存放我的檢視.
所以我通過實現IViewLocationExpander進行擴充套件新增我自定義檢視路徑規則即可正如下程式碼片段
public class MyViewLocationExpander : IViewLocationExpander { public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context,IEnumerable<string> viewLocations) { if (context.ControllerName != null && context.ControllerName.StartsWith("App")) { viewLocations = viewLocations.Concat( new[] { $"/Areas/sysManage/Views/App/{context.ControllerName}/{context.ViewName}{RazorViewEngine.ViewExtension}" }); return viewLocations; } if (context.AreaName != "sysManage") return viewLocations; viewLocations = viewLocations.Concat( new[] { $"/Areas/sysManage/Views/System/{context.ControllerName}/{context.ViewName}{RazorViewEngine.ViewExtension}" }); return viewLocations; } public void PopulateValues(ViewLocationExpanderContext context) { } }
在Startup.ConfigureServices 註冊
public void ConfigureServices(IServiceCollection services) { services.Configure<RazorViewEngineOptions>(o => { o.ViewLocationExpanders.Add(new MyViewLocationExpander()); }); services.AddMvc(); }
app.UseEndpoints(endpoints => { endpoints.MapRazorPages(); endpoints.MapAreaControllerRoute( name: "sysManage","sysManage",pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}"); });
最終路由指向的還是
/SysManage/Controller/Action
到此這篇關於ASP.NET Core MVC通過IViewLocationExpander擴充套件檢視搜尋路徑的實現的文章就介紹到這了,更多相關ASP.NET Core MVC 擴充套件檢視搜尋路徑內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!