Convert HttpRequestMessage to HttpRequest

24,359

You probably don't need to convert from HttpRequestMessage. There is another way:

var context = new HttpContextWrapper(HttpContext.Current);
HttpRequestBase request = context.Request;
Share:
24,359
Avi L
Author by

Avi L

I'm a lead developer in the Software/Internet industry with vast experience as project manager, technical leader and senior developer from 1998. Rain-Maker, self-motivated, resourceful, adaptive, organized, passionate entrepreneur attitude with strong combination of business skills and hands on technical expertise. Specialties: A dynamic leader, great technical and architectural skills, confident, intuitive, high communication skills, Very creative and innovate personality and able to drive decisions and changes. Vast experience in online gaming, online commerce, development and project management.

Updated on July 09, 2022

Comments

  • Avi L
    Avi L almost 2 years

    I'm converting ASP.NET web application to MVC Web Api project. One of my methods expects HttpRequest class. However, Web Api controller holds only HttpRequestMessage (this.Request) object. Do you know how I can convert HttpRequestMessage to HttpRequest?

  • Avi L
    Avi L about 11 years
    How do I get an 'HttpRequestBase'? should I create one myself? (i.e. create new object and copy 'HttpRequestMessage' parameters to it)
  • sobelito
    sobelito over 10 years
    System.Web.HttpContext.Current.Request
  • Andy
    Andy almost 8 years
    This seems awfully like cheating to me but I'm forced to do it if I want to write functions that can be called from ASP.NET Web API controllers as well as ASP.NET MVC controllers. For the life of me I don't understand by web API couldn't just be built on top of MVC (so ApiController could be a subclass of a normal MVC Controller). Instead if you want to use MVC and API together you have to navigate between two parallel but incompatible worlds with the same concepts in each.
  • OutstandingBill
    OutstandingBill about 6 years
    I've seen on the comments under another answer that you shouldn't use System.Web.HttpContext.Current in Web API projects - you can instead use (HttpContextWrapper)Request.Properties["MS_HttpContext"]
  • CSharper
    CSharper over 5 years
    you can get HttpRequestBase using ((HttpContextWrapper)Request.Properties["MS_HttpContext"]).R‌​equest