dnolan
dnolan

Reputation: 2409

Accessing AspNetRequest

I have a global filters which adds a token to the request.Items collection which subsequent filters can also access.

The problem I am having is when trying to get a hold of the request.Items when I am within an injected class.

When trying to access the Items array this injected class using var req = HostContext.TryGetCurrentRequest(); my item is no longer available. I have discovered that this is because in the filters, the provided IRequest.Items array is a cloned copy of HttpContext.Current.Items within an AspNetRequest object, so anything I add is not placed into HttpContext.Current.Items.

So my question is, how do I access or inject the current AspNetRequest.Items array in this situation, or should I just add my item to HttpContext.Current.Items directly?

See https://github.com/ServiceStack/ServiceStack/blob/master/src/ServiceStack/Host/AspNet/AspNetRequest.cs#L53 for the clone.

Upvotes: 1

Views: 64

Answers (1)

mythz
mythz

Reputation: 143284

You can get the underlying ASP.NET Request from the IRequest with:

var aspReq = (HttpRequestPase)req.OriginalRequest;
var item = aspReq.Items["item"];

Otherwise you could also use the HttpContext.Current.Items singleton.

Upvotes: 2

Related Questions