ASP.NET Core MVC通過IViewLocationExpander擴展視圖搜索路徑的實現(xiàn)
IViewLocationExpander API
- ExpandViewLocations Razor視圖路徑,視圖引擎會搜索該路徑.
- PopulateValues 每次調用都會填充路由
項目目錄如下所示
創(chuàng)建區(qū)域擴展器,其實我并不需要多區(qū)域,我目前只需要達到一個區(qū)域中有多個文件夾進行存放我的視圖.
所以我通過實現(xiàn)IViewLocationExpander進行擴展添加我自定義視圖路徑規(guī)則即可正如下代碼片段
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擴展視圖搜索路徑的實現(xiàn)的文章就介紹到這了,更多相關ASP.NET Core MVC 擴展視圖搜索路徑內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Asp.Net 生成靜態(tài)頁并實現(xiàn)分頁效果
Asp.Net 生成靜態(tài)頁并實現(xiàn)分頁效果的代碼,需要的朋友可以參考下。2010-04-04IIS 瀏覽aspx頁面出現(xiàn)無法顯示XML頁的解決方法分享
這篇文章介紹了IIS 瀏覽aspx頁面出現(xiàn)無法顯示XML頁的解決方法,有需要的朋友可以參考一下2013-11-11Asp.Net、asp實現(xiàn)的搜索引擎網(wǎng)址收錄檢查程序
這篇文章主要介紹了Asp.Net、asp實現(xiàn)的搜索引擎網(wǎng)址收錄檢查程序,即實現(xiàn)檢查一個網(wǎng)址是否被搜索引擎收錄功能的小程序,需要的朋友可以參考下2014-08-08asp.net MVC 在Controller控制器中實現(xiàn)驗證碼輸出功能
這篇文章主要介紹了asp.net MVC 在Controller控制器中實現(xiàn)驗證碼輸出功能,本文給大家介紹非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-12-12