ASP.NET Core MVC通过IViewLocationExpander扩展视图搜索路径
IViewLocationExpander APIExpandViewLocations Razor视图路径,视图引擎会搜索该路径.PopulateValues 每次调用都会填充路由项目目录如下所示创建区域扩展器,其实我并不需要多区域,我目前只需要达到一个区域中有多个文件夹进行存放我的视图.所以我通过实现IViewLocationExpander进行扩展添加我自定义视图路径规则即可正如下代码片段p
·
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自定义View
查找路径,实现主题切换(IViewLocationExpander
)
目的:修改视图的查找路径
PopulateValues
方法:每次http
请求都会执行
ExpandViewLocations
方法:根据context.Values
的值缓存执行。相同值,此方法只会执行一次
viewLocations
参数:默认查找路径
方案1:
案例中使用Query
参数中获取theme
,项目中可以改成从配置文件中获取(在中间件中修改,通过HttpContext.Items
传递参数)
public class ThemeViewLocationExpander : IViewLocationExpander
{
public void PopulateValues(ViewLocationExpanderContext context)
{
string theme = context.ActionContext.HttpContext.Request.Query["theme"].ToString();
context.Values["theme"] = theme;
}
//有缓存,context.Values["theme"]相同值只会执行一次此方法
public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations)
{
string theme = context.Values["theme"];
if (string.IsNullOrWhiteSpace(theme))
{
theme = "default";
}
string[] newLocation = { $"Views/{theme}/{{1}}/{{0}}.cshtml" };//2代表Area、1代表Controller、0代表Action
return viewLocations.Union(newLocation);
}
}
ConfigureServices
方法中添加:
//配置模版视图路径
services.Configure<RazorViewEngineOptions>(options =>
{
options.ViewLocationExpanders.Add(new ThemeViewLocationExpander());
});
方案2
services.Configure<RazorViewEngineOptions>(options =>
{
第二种定位自定义页面文件的位置
//options.AreaViewLocationFormats.Clear();
//options.AreaViewLocationFormats.Add("/Areas/{2}/Views/{1}/{0}.cshtml");
//options.AreaViewLocationFormats.Add("/Areas/{2}/Views/Shared/{0}.cshtml");
//options.AreaViewLocationFormats.Add("/Views/Shared/{0}.cshtml");
});
更多推荐
已为社区贡献24条内容
所有评论(0)