How do I convert an HttpRequest into an HttpRequestBase object?

匿名 (未验证) 提交于 2019-12-03 02:45:02

问题:

My problem is the opposite of this: How do I convert an HttpRequestBase into an HttpRequest object?

In my ASP.NET MVC application I have a method used by many controllers that receive an HttpRequestBase as argument.

Now I have to call that method from another method, that is not an action (it's an nhibernate interceptor). In this second method I could access HttpContext.Current.Request, that is a HttpRequest and I cannot cast it to HttpRequestBase (I thought it was possibile due to the naming ...).

Does someone know in what relationship are this classes and how can I solve my problem? Thank you.

回答1:

You'll want to wrap your HttpRequest in a HttpRequestWrapper:

var wrapper = new HttpRequestWrapper(httpRequest); 

The HttpRequestWrapper inherits from HttpRequestBase.



回答2:

Alternative solution which does not require to create a new instance:

var httpRequestBase = httpRequest.RequestContext.HttpContext.Request; 

I have tested this in MVC 5.



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