How do I convert an HttpRequestBase into an HttpRequest object?

79,439

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.

Share:
79,439
Pure.Krome
Author by

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, 2020

Comments

  • Pure.Krome
    Pure.Krome almost 3 years

    inside my ASP.NET MVC controller, I've got a method that requires an HttpRequest object. All I have access to is an HttpRequestBase object.

    Is there anyway I can somehow convert this?

    What can/should I do??

  • Pure.Krome
    Pure.Krome over 13 years
    Totally 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
    Pure.Krome over 13 years
    Embarassingly, 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
    Sunil486 over 13 years
    To be sure you are grabbing the HttpContext class instead of the controller member, try and use System.Web.HttpContext.Current.
  • Pure.Krome
    Pure.Krome over 13 years
    I 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
    Chris Moschini almost 11 years
    Link is dead; developmentalmadness.com domain expired, GoDaddy filler page now
  • Sunil486
    Sunil486 almost 11 years
    Link to Google Cache of that page: webcache.googleusercontent.com/…
  • CountZero
    CountZero about 10 years
    Not true.var httpRequest = Context.Request; var httpRequestBase = new HttpRequestWrapper(Context.Request);
  • Junle Li
    Junle Li over 8 years
    Another note that, not only use HttpRequestBase and HttpResponseBase, also HttpContextBase. :)
  • Krisztián Balla
    Krisztián Balla almost 8 years
    System.Web.HttpContext.Current.Request
  • Suncat2000
    Suncat2000 over 2 years
    That's converting in the wrong direction. The question was: if I have a HttpRequestBase, how do I get an actual HttpRequest from it?