克隆当前请求的 HttpContext 实例的最简单方法是什么?
我正在开发一个应用程序Asp.net MVC v1。我升级了常规 PartialView 功能,使其实际上具有行为非常相似的子控制器,但具有自己的上下文。当您使用 PartialViews 时,您必须在主视图的控制器操作中填充部分视图的视图数据。我创建了自己的功能,可以从视图内调用控制器操作。这样我得到:
- 我不必在主视图的控制器操作中提供子视图的数据
- 子控制器方法可以更封装地操作数据,与其他视图/控制器没有任何关系
问题是每个子控制器请求都使用HttpContext。因此,当我在子控制器中设置一些 HttpContext.Item 时,它实际上会填充实际请求的 HttpContext。
这就是为什么我想克隆 HttpContext。我已经在使用:
HttpContext subContext = new HttpContext(request, response);
// what happened to Session, User, Items etc. properties?
但这除了请求和响应之外没有设置任何其他内容。但我可能还需要其他属性和集合...比如会话、项目、用户...等。
虽然“不可能”的答案是正确的,但还有一种替代方案比将值写入当前上下文然后重写回其原始状态要干净得多。解决方案是完全基于您选择的 URL 创建一个新的 HttpContext 对象。
// A new request/response is constructed to using a new URL.
// The new response is using a StreamWriter with null stream as a backing stream
// which doesn't consume resources
using (var nullWriter = new StreamWriter(Stream.Null))
{
var newRequestUri = new Uri("http://www.somewhere.com/some-resource/");
var newRequest = new HttpRequest("", newRequestUri.ToString(), newRequestUri.Query);
var newResponse = new HttpResponse(nullWriter);
var newContext = new HttpContextWrapper(new HttpContext(newRequest, newResponse));
// Work with the new context here before it is disposed...
}
参考:https://github.com/maartenba/MvcSiteMapProvider/issues/278#issuecomment-34905271 https://github.com/maartenba/MvcSiteMapProvider/issues/278#issuecomment-34905271
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)