MVC Razor ViewEngine 不是线程安全的?
我目前有一个 .net mvc3 应用程序,负责管理多个位置的类似视图,这些视图也使用相同的控制器,例如 site1/v1/views/index.cshtml
和 site1/v2/views /index.cshtml
。
处理此问题的方法是创建一个继承自 DefaultControllerFactory 的 CustomControllerFactory,并在 CreateController 方法中清除现有视图引擎并添加一个新的自定义 viewEngine,该视图引擎根据当前 url 指定视图位置格式。
如果用户登陆 site1.com/v1/index.cshtml
,视图引擎将指定以下视图位置:
string versionDirectory = "v1";
ViewLocationFormats = new[]{ versionDirectory + "/Views/{0}.cshtml",
"/Views/{0}.cshtml",
"~/Shared/{0}.cshtml"
};
我遇到的问题是,如果多个用户在大致相同的位置登陆不同页面时间所有用户都会看到相同的视图。
最初我认为这与缓存有关,但在显式设置 usecache = 后false
在自定义 viewEngine 中,似乎这与 ViewEngines 类不是线程安全的。
有谁知道我如何以不同的方式实现相同的结果?
提前致谢。
I currently have a .net mvc3 application that is responsible for managing similar views in multiple locations that also use the same controllers e.g site1/v1/views/index.cshtml
and site1/v2/views/index.cshtml
.
The way that this is handled is by creating a CustomControllerFactory that inherits from DefaultControllerFactory and in the CreateController method, clear the existing view engines and add a new custom viewEngine that specifies the view location formats based off the current url.
If the user lands on site1.com/v1/index.cshtml
, the viewengine will specify the view locations of :
string versionDirectory = "v1";
ViewLocationFormats = new[]{ versionDirectory + "/Views/{0}.cshtml",
"/Views/{0}.cshtml",
"~/Shared/{0}.cshtml"
};
The problem that I am having is that if multiple users land on different pages at roughly the same time all the users will see the same view.
Initially i thought this was related to caching, but after explicitly setting usecache = false
in the custom viewEngine, it seems like this has got more to do with the ViewEngines class not being thread safe.
Does anyone have any ideas about how I can accomplish a the same result, but in a different way?
thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
ViewEngines 集合是一个静态集合,因此它的值在请求之间共享。您尝试做的事情是可能的,但您这样做的方式不正确。
一种简单的方法是编写一个派生自 RazorViewEngine 的自定义视图引擎并重写 FindView 方法。每个请求都会调用该方法一次。在您的实现中,调用 base.FindView,然后修改结果(如果它不为空)以包含您需要的站点信息。
Scott Hanselman 有一篇博客文章,展示了通过自定义视图引擎在另一个位置查找视图的示例。 http://www.hanselman.com/blog/ABetterASPNETMVCMobileDeviceCapabilityViewEngine.aspx
The ViewEngines collection is a static collection and thus its values are shared across requests. What you are attempting to do is possible, but the way you are doing it is not correct.
One easy approach is to write a custom view engine that derives from the RazorViewEngine and override the FindView method. That method is called once per request. In your implementation, call base.FindView and then modify the result (if it's not null) to include the site information you need.
Scott Hanselman has a blog post that shows one example of looking in another location for views via a custom view engine. http://www.hanselman.com/blog/ABetterASPNETMVCMobileDeviceCapabilitiesViewEngine.aspx
内置视图引擎是线程安全的。您的问题是缓存或自定义视图引擎。检查其他答案中链接中的缓存注释。
The built-in view engines ARE thread-safe. Your problem is caching or your custom view-engine. Check the caching comments in the link in the other answer.