您现在的位置是:网站首页> 编程资料编程资料
ASP.NET Core MVC通过IViewLocationExpander扩展视图搜索路径的实现_实用技巧_
2023-05-24
367人已围观
简介 ASP.NET Core MVC通过IViewLocationExpander扩展视图搜索路径的实现_实用技巧_
IViewLocationExpander API
- ExpandViewLocations Razor视图路径,视图引擎会搜索该路径.
- PopulateValues 每次调用都会填充路由
项目目录如下所示

创建区域扩展器,其实我并不需要多区域,我目前只需要达到一个区域中有多个文件夹进行存放我的视图.
所以我通过实现IViewLocationExpander进行扩展添加我自定义视图路径规则即可正如下代码片段
public class MyViewLocationExpander : IViewLocationExpander { public IEnumerable ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable 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(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 扩展视图搜索路径内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
您可能感兴趣的文章:
相关内容
- .NET Core中创建和使用NuGet包的示例代码_实用技巧_
- 从ASP.NET Core3.1迁移到5.0的方法_实用技巧_
- IdentityServer4实现.Net Core API接口权限认证(快速入门)_实用技巧_
- asp.net core3.1 引用的元包dll版本兼容性问题解决方案_实用技巧_
- asp.net通过消息队列处理高并发请求(以抢小米手机为例)_实用技巧_
- .Net Core 实现图片验证码的实现示例_实用技巧_
- 浅谈ASP.NET Core 中jwt授权认证的流程原理_实用技巧_
- Net Core Web Api项目与在NginX下发布的方法_实用技巧_
- 详解.NET Core 3.0 里新的JSON API_实用技巧_
- 使用.net core3.0 正式版创建Winform程序的方法(图文)_实用技巧_
