I'm calling a third-party library that takes a System.Web.HttpResponse
. I see I have a HttpResponseBase
, but not HttpResponse
like in web forms.
我正在调用一个带有System.Web.HttpResponse的第三方库。我看到我有一个HttpResponseBase,但不像Web表单中的HttpResponse。
Is there a way to get the HttpResponse
? Using MVC 3
.
有没有办法获得HttpResponse?使用MVC 3。
[Edit] : I'm trying to do this in a controller method. Also corrected casing.
[编辑]:我试图在控制器方法中这样做。还纠正了套管。
3 个解决方案
#1
31
If you need to interact with systems which take the non-mockable types, you can get access to the current HttpContext via the static property System.Web.HttpContext.Current. The HttpResponse is just hanging off of there via the Response property.
如果需要与采用非可模拟类型的系统进行交互,则可以通过静态属性System.Web.HttpContext.Current访问当前的HttpContext。 HttpResponse只是通过Response属性挂在那里。
#2
9
In mvc application you can use HttpContext.ApplicationInstance.Response
.This helped me for getting the HttpResponse
in MVC Application.
在mvc应用程序中,您可以使用HttpContext.ApplicationInstance.Response.This帮助我获取MVC应用程序中的HttpResponse。
#3
4
No, but your HttpResponseBase
is probably an HttpResponseWrapper
which contains an HttpResponse
inside of it. All the HttpResponse
methods are accessible from the HttpResponseBase
.
不,但你的HttpResponseBase可能是一个HttpResponseWrapper,里面包含一个HttpResponse。可以从HttpResponseBase访问所有HttpResponse方法。
If you want access to the HttpResponse
, then you could add a reference to it in HttpContext.Items
in your IHttpHandler
or somewhere earlier in the ASP.NET lifecycle. The BeginRequest event would be a good point to do this.
如果要访问HttpResponse,则可以在IHttpHandler中的HttpContext.Items或ASP.NET生命周期早期的某处添加对它的引用。 BeginRequest事件是一个很好的做法。
Your HttpContext.Items
references the same dictionary that HttpContextBase.Items
references, so you will have access to all those items in MVC3
您的HttpContext.Items引用了HttpContextBase.Items引用的相同字典,因此您可以访问MVC3中的所有项目
To clarify,
It is an HttpResponseWrapper
, but there is no public accessor for the HttpResponse
. So, there is not a directly accessible reference. To make a directly accessible reference before the framework decides to start giving you the wrapper instead of the underlying reference, create an event handler for HttpApplication.BeginRequest
event. Your handler will have a reference to the HttpContext
object. Set HttpContext.Items["HttpRequest"] = HttpContext.Request
. Then in your controller you will be able to access the HttpRequest
reference by RequestContext.HttpContext.Items["HttpRequest"]
.
它是一个HttpResponseWrapper,但没有HttpResponse的公共访问器。因此,没有可直接访问的参考。要在框架决定开始为您提供包装器而不是底层引用之前创建可直接访问的引用,请为HttpApplication.BeginRequest事件创建一个事件处理程序。您的处理程序将具有对HttpContext对象的引用。设置HttpContext.Items [“HttpRequest”] = HttpContext.Request。然后在您的控制器中,您将能够通过RequestContext.HttpContext.Items [“HttpRequest”]访问HttpRequest引用。
#1
31
If you need to interact with systems which take the non-mockable types, you can get access to the current HttpContext via the static property System.Web.HttpContext.Current. The HttpResponse is just hanging off of there via the Response property.
如果需要与采用非可模拟类型的系统进行交互,则可以通过静态属性System.Web.HttpContext.Current访问当前的HttpContext。 HttpResponse只是通过Response属性挂在那里。
#2
9
In mvc application you can use HttpContext.ApplicationInstance.Response
.This helped me for getting the HttpResponse
in MVC Application.
在mvc应用程序中,您可以使用HttpContext.ApplicationInstance.Response.This帮助我获取MVC应用程序中的HttpResponse。
#3
4
No, but your HttpResponseBase
is probably an HttpResponseWrapper
which contains an HttpResponse
inside of it. All the HttpResponse
methods are accessible from the HttpResponseBase
.
不,但你的HttpResponseBase可能是一个HttpResponseWrapper,里面包含一个HttpResponse。可以从HttpResponseBase访问所有HttpResponse方法。
If you want access to the HttpResponse
, then you could add a reference to it in HttpContext.Items
in your IHttpHandler
or somewhere earlier in the ASP.NET lifecycle. The BeginRequest event would be a good point to do this.
如果要访问HttpResponse,则可以在IHttpHandler中的HttpContext.Items或ASP.NET生命周期早期的某处添加对它的引用。 BeginRequest事件是一个很好的做法。
Your HttpContext.Items
references the same dictionary that HttpContextBase.Items
references, so you will have access to all those items in MVC3
您的HttpContext.Items引用了HttpContextBase.Items引用的相同字典,因此您可以访问MVC3中的所有项目
To clarify,
It is an HttpResponseWrapper
, but there is no public accessor for the HttpResponse
. So, there is not a directly accessible reference. To make a directly accessible reference before the framework decides to start giving you the wrapper instead of the underlying reference, create an event handler for HttpApplication.BeginRequest
event. Your handler will have a reference to the HttpContext
object. Set HttpContext.Items["HttpRequest"] = HttpContext.Request
. Then in your controller you will be able to access the HttpRequest
reference by RequestContext.HttpContext.Items["HttpRequest"]
.
它是一个HttpResponseWrapper,但没有HttpResponse的公共访问器。因此,没有可直接访问的参考。要在框架决定开始为您提供包装器而不是底层引用之前创建可直接访问的引用,请为HttpApplication.BeginRequest事件创建一个事件处理程序。您的处理程序将具有对HttpContext对象的引用。设置HttpContext.Items [“HttpRequest”] = HttpContext.Request。然后在您的控制器中,您将能够通过RequestContext.HttpContext.Items [“HttpRequest”]访问HttpRequest引用。