views:

274

answers:

3

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

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:

  • I don't have to provide sub-view's data in my main view's controller action
  • sub controller methods can manipulate data more encapsulated without any relation to other views/controllers

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.

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.

A: 

The ASP.NET MVC framework intentionally makes dependencies to abstract classes with all members virtual. That simply says - extensibility.

Controllers depend on HttpContextBase, not HttpContext. Perhaps you can make your sub-controllers depend on HttpContextBase too so you can wrap it. Just my 2 cents.

Canton
Actually I do create a HttpContextBase (HttpContextWrapper actually that is a child class of HttpContextBase) but it still doesn't solve my problem. This base just wraps original context, doesn't it? I guess that's the idea with this class.
Robert Koritnik
A: 

I've used

<% Html.RenderAction("Action", "Controller"); %>

to great effect, allowing me to create completely isolated/escapsulated actions without resorting to complex code. This would seem to offer the same functionality without the same complexity.

The rendered views are standard partial views and the controller actions just like any other.

Lazarus
I have my my own implementation of this capability because the one you're talking about doesn't work as expected when various Http verbs are used etc. If you make a POST all sub actions will also see it as a post even though they shouldn't be. And other particularities (with validation etc)... So my advanced version upgrades this system and for it I need to clone HttpContext to make it work as really independant request.
Robert Koritnik
A: 

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.

Robert Koritnik