栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > asp

ASP.NET Core MVC通过IViewLocationExpander扩展视图搜索路径的实现

asp 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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 扩展视图搜索路径内容请搜索考高分网以前的文章或继续浏览下面的相关文章希望大家以后多多支持考高分网!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/54960.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号