Accessing AspNetRequest

半腔热情 提交于 2021-01-29 03:09:10

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/38922776/accessing-aspnetrequest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!