是否可以复制/克隆Web请求的HttpContext [英] Is it possible to copy/clone HttpContext of a web request

查看:224
本文介绍了是否可以复制/克隆Web请求的HttpContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

克隆当前请求的HttpContext实例的最简单方法是什么?

What's the easiest way to clone current request's HttpContext instance?

我正在 Asp.net MVC v1 中开发应用程序。我升级了常规的PartialView功能,以使子控制器的功能非常相似,但是具有各自的上下文。使用PartialViews时,您必须在主视图的控制器操作中填充部分视图的视图数据。我创建了自己的功能,可以从视图中调用控制器动作。这样我得到:

I'm developing an app in Asp.net MVC v1. I upgraded the regular PartialView capabilities to actually have sub-controllers that act very similar, but have their own context. When you use PartialViews you have to fill view data for the partial view in your main view's controller action. I created my own functionality that makes it possible to call controller actions from within a view. This way I get:


  • 我不必在主视图的控制器操作中提供子视图的数据

  • sub控制器方法可以操作更多封装的数据,而与其他视图/控制器没有任何关系

问题在于每个子-controller请求使用HttpContext。因此,当我在子控制器中设置一些HttpContext.Item时,它实际上会填充实际请求的HttpContext。

The problem is that each sub-controller request uses HttpContext. So when I set some HttpContext.Item in a sub-controller it actually populates HttpContext of the actual request.

这就是为什么我要克隆HttpContext的原因。我已经在使用:

That's why I want to clone HttpContext. I'm already using:

HttpContext subContext = new HttpContext(request, response);
// what happened to Session, User, Items etc. properties?

但这不设置请求和响应以外的任何内容。但是我可能还需要其他属性和集合...例如会话,项目,用户...等。

but this doesn't set anything else than request and response. But I would probably also need other properties and collections... Like Session, Items, User... etc.

推荐答案

不是可能



我想由于服务器会话状态的原因,无法进行实际的深度克隆。克隆还必须克隆此值,该值是Web服务器特定的内部资源,其本质上是静态的,无法克隆。在这种情况下,Web服务器将具有多个会话对象。

Not possible

I guess an actual deep cloning is not possible because of server session state. Cloning would also have to clone this value, which is web server specific internal resource that is intrinsically static and can not be cloned. In this case a web server would have multiple Session objects for instance.

解决方法

无论如何。解决方法是在实例化子控制器处理之前设置其他上下文值。处理完成后,我将值恢复为原始值。所以我实际上和以前一样有上下文。

Workaround
Anyway. The workaround was to set additional context values before instantiating sub-controller processing. After processing is finished I reverted values back to original. So I actually had context as it was before.

这篇关于是否可以复制/克隆Web请求的HttpContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆