webworm
webworm

Reputation: 11019

How to obtain HttpRequestMessage outside of controller

I have a Web API v2 controller that makes a method call out to a service in the same project. The service uses the UrlHelper class to put together a URL however, the UrlHelper class needs the HttpRequestMessage in order to build the proper URL.

How can I obtain the HttpRequestMessage from within a class (my service) that is outside of the API controller? Can I pass the HttpRequestMessage into the service method? Is there another way to access from a global context?

I am using a self-hosted environment (OWIN).

Upvotes: 0

Views: 1363

Answers (1)

Dai
Dai

Reputation: 155085

public class FooController : ApiController
{
    public IHttpActionResult BarAction()
    {
        UrlHelper urlHelper = GetUrlHelperFromWhereever();
        urlHelper.DoSomething( this.Request );
    }

this.Request is HttpRequestMessage: https://msdn.microsoft.com/en-us/library/system.web.http.apicontroller.request(v=vs.118).aspx#P:System.Web.Http.ApiController.Request

Upvotes: 2

Related Questions