IViewLocationExpander API
項目目錄如下所示
創建區域擴展器,其實我并不需要多區域,我目前只需要達到一個區域中有多個文件夾進行存放我的視圖.
所以我通過實現IViewLocationExpander進行擴展添加我自定義視圖路徑規則即可正如下代碼片段
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
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 注冊
1
2
3
4
5
6
7
|
public void ConfigureServices(IServiceCollection services) { services.Configure<RazorViewEngineOptions>(o => { o.ViewLocationExpanders.Add( new MyViewLocationExpander()); }); services.AddMvc(); } |
1
2
3
4
5
6
7
|
app.UseEndpoints(endpoints => { endpoints.MapRazorPages(); endpoints.MapAreaControllerRoute( name: "sysManage" , "sysManage" , pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}" ); }); |
最終路由指向的還是
1
|
/SysManage/Controller/Action |
到此這篇關于ASP.NET Core MVC通過IViewLocationExpander擴展視圖搜索路徑的實現的文章就介紹到這了,更多相關ASP.NET Core MVC 擴展視圖搜索路徑內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://www.cnblogs.com/yyfh/p/12636976.html