ASP.NET Core MVC通过IViewLocationExpander扩展视图搜索路径的实现
程序员文章站
2022-03-21 12:30:48
iviewlocationexpander api expandviewlocations razor视图路径,视图引擎会搜索该路径. populatevalues 每次调用都会填充路由项目目录如...
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 扩展视图搜索路径内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!