asp.net - 动态自定义 RazorViewEngine?
问题描述
我们有一个RazorViewEngine
基于主题的定制。
public CustomViewEngine(string theme)
{
PartialViewLocationFormats = new[]
{
"~/Views/PartialViews/" + theme + "/{0}.cshtml",
"~/Views/PartialViews/Base/{0}.cshtml"
}; // This is simplified, we actually have some themes falling back to some other themes before falling back to Base
}
protected void Application_Start()
{
string theme = GetTheme(); // read from config file
ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new CustomViewEngine(theme));
}
当主题是静态的(例如,来自配置文件并且从不更改)时,这一切都很好。
但是现在我们需要主题是动态的(用户将能够更改它)。
这样做的最佳方法是什么?
如果请求一次出现一个,则设置ViewEngine
页面加载(在 中Controller
,而不是在 中Application_Start
)有效,但我担心当人们同时点击页面时它可能会加载错误的主题。
public class HomeController : Controller
{
public ActionResult Index()
{
string selectedTheme = GetUserTheme(); // eg. HttpContext.Current.Request["theme"]
// Reset ViewEngine every page load because selectedTheme may have changed
ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new CustomViewEngine(selectedTheme));
// Putting Thread.Sleep here (to simulate concurrent requests) and opening multiple
// tabs with different theme selections will make some tabs load the wrong theme :(
return View();
}
}
您如何正确CustomViewEngine
选择正确的主题并在并发请求上保持稳健?
或者有没有办法覆盖ViewEngine 逻辑,以便我们可以编写自己的函数来定位 .cshtml 文件(而不是只传递可能的文件位置数组)?
编辑:
显然解决方案是覆盖FileExists
,尽管它会使页面加载速度变慢。
protected override bool FileExists(ControllerContext controllerContext, string virtualPath)
{
// Do your own logic here, look up Request and return true or false
return base.FileExists(controllerContext, virtualPath);
}
http://robhead89.blogspot.com/2014/01/aspnet-viewengine-caching-and-how-to.html
解决方案
推荐阅读
- angular - 如何使用 ngrx/data saveEntities 添加多个实体进行存储?
- r - 排序数据以匹配 ggplot 字体
- javascript - 如何从 youtube 获取调试视频数据的播放质量?
- rust - 如何为字段编写具有生命周期约束的函数?
- r - packrat::resolve() 一直要求我在 Rtools 已经安装时安装它
- admob - 在禁用 c++ 模块时使用“@import”,考虑使用 -fmodules 和 -fcxx-modules
- python - Edge 的关键属性含义 OSMNX
- javascript - JavaScript DOM 音频未播放
- c# - 如何确保在 CefSharp 中执行 ExecuteScriptAsync
- vue.js - 商店用品 6 | 自定义字段为空