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.
In my application I had calls coming from several different places that needed access to the HttpRequestBase. I created this set of extension methods to get and convert from several different Http types into a HttpRequestBase, then used HttpRequestBase as the base type for interface and class methods through the application when I needed access to the request.
I came across several SO answers that helped me build these extensions:
You'll want to wrap your
HttpRequest
in aHttpRequestWrapper
:The
HttpRequestWrapper
inherits fromHttpRequestBase
.I find the following extension methods useful:
Usage:
Alternative solution which does not require to create a new instance:
I have tested this in MVC 5.