How do I convert an HttpRequestBase into an HttpRequest object?
Solution 1
Is it your method, so you can re-write it to take HttpRequestBase? If not, you can always get the current HttpRequest from HttpContext.Current.HttpRequest to pass on. However, I often wrap access to the HttpContext inside a class like mentioned in ASP.NET: Removing System.Web Dependencies for better unit testing support.
Solution 2
You should always use HttpRequestBase and HttpResponseBase in your application as opposed to the concrete versions which are impossible to test (without typemock or some other magic).
Simply use the HttpRequestWrapper class to convert as shown below.
var httpRequestBase = new HttpRequestWrapper(Context.Request);
Solution 3
You can just use
System.Web.HttpContext.Current.Request
The key here is that you need the full namespace to get to the "correct" HttpContext.
I know it's been 4 years since this question was asked, but if this will help somebody, then here you go!
(Edit: I see that Kevin Hakanson already gave this answer...so hopefully my response will help those people who just read answers and not comments.) :)
Solution 4
To get HttpRequest in ASP.NET MVC4 .NET 4.5, you can do the following:
this.HttpContext.ApplicationInstance.Context.Request
Solution 5
Try to use/create a HttpRequestWrapper using your HttpRequestBase.
Pure.Krome
Just another djork trying to ply his art in this mad mad world. Tech stack I prefer to use: Laguage: C# / .NET Core / ASP.NET Core Editors: Visual Studio / VS Code Persistence: RavenDB, SqlServer (MSSql or Postgres) Source control: Github Containers: Docker & trying to learn K&'s Cloud Platform: Azure Caching/CDN: Cloudflare Finally: A Tauntaun sleeping bag is what i've always wanted spaces > tabs
Updated on July 10, 2020Comments
-
Pure.Krome almost 3 yearsinside my ASP.NET MVC controller, I've got a method that requires an
HttpRequestobject. All I have access to is anHttpRequestBaseobject.Is there anyway I can somehow convert this?
What can/should I do??
-
Pure.Krome over 13 yearsTotally agreed! problem is, I can't modify the current class library we're required to use .. so this doesn't help me much :( -
Pure.Krome over 13 yearsEmbarassingly, I also thought of this and it doesn't work. The HttpContext is the MVC context .. so there is no 'Current' property exposed on it. I'm not sure how to get access to 'oldschool' HttpContext.Current ... ??? -
Sunil486 over 13 yearsTo be sure you are grabbing the HttpContext class instead of the controller member, try and use System.Web.HttpContext.Current. -
Pure.Krome over 13 yearsI needed to use the full namespace because it was taking the current MVC namespace property. cheers. Note to others: don't do what i'm doing. it's a VeryBadThing(tm). -
Chris Moschini almost 11 yearsLink is dead; developmentalmadness.com domain expired, GoDaddy filler page now -
Sunil486 almost 11 yearsLink to Google Cache of that page: webcache.googleusercontent.com/… -
CountZero about 10 yearsNot true.var httpRequest = Context.Request; var httpRequestBase = new HttpRequestWrapper(Context.Request); -
Junle Li over 8 yearsAnother note that, not only useHttpRequestBaseandHttpResponseBase, alsoHttpContextBase. :) -
Krisztián Balla almost 8 yearsSystem.Web.HttpContext.Current.Request -
Suncat2000 over 2 yearsThat's converting in the wrong direction. The question was: if I have aHttpRequestBase, how do I get an actualHttpRequestfrom it?