ASP.NET Core MVC通過IViewLocationExpander擴展視圖搜索路徑的實現(xiàn)
IViewLocationExpander API
- ExpandViewLocations Razor視圖路徑,視圖引擎會搜索該路徑.
- PopulateValues 每次調(diào)用都會填充路由
項目目錄如下所示

創(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下用Aspose.Words for .NET動態(tài)生成word文檔中的圖片或水印的方法
本文詳細講解如何使用Aspose.Words for .NET的組件來生成word文檔與水印的方法,請看本文內(nèi)容。2010-04-04
Asp.net控制Tomcat啟動關閉的實現(xiàn)方法
近日有個項目客戶要求能自己配置相關權限。由于歷史原因這個項目采用的是公司以前的權限系統(tǒng)2012-01-01
Asp.net中static變量和viewstate的使用方法(謹慎)
如在頁面中統(tǒng)計某個按鈕被按下的次數(shù),先在類中OnClick事件的處理過程前定義一static變量times,則每次調(diào)用該按鈕的OnClick事件時,令times增1即可,非常方便,接下來詳細介紹,感興趣的朋友可以了解下2013-01-01
asp.net GridView控件鼠標移動某行改變背景顏色(方法一)
asp.net GridView控件鼠標移動某行改變背景顏色2009-12-12

