How to get HttpRequestBase from IOwinContext

I started using Owin host itself for my API and now I am trying to fix some tests that started to fail because Owin does not support HttpContext.Current

Now I am stuck in getting HttpRequestBase

from IOwinContext

. Here is my old code that I used before Owin:

public static HttpRequestBase GetRequestBase(this HttpRequestMessage request)
{
    return ((HttpContextWrapper)request.Properties["MS_HttpContext"]).Request;
}

      

And here's my attempt based on this answer :

public static HttpRequestBase GetRequestBase(this HttpRequestMessage request)
{
    var context = request.GetOwinContext();

    HttpContextBase httpContext = context.Get<HttpContextBase>(typeof(HttpContextBase).FullName); // <---- Returns null

    return httpContext.Request;
}

      

The problem is the variable is httpContext

returning null

and I don't know what is wrong.

Does anyone know how to get the HttpRequestBase using Owin?

+3


source to share


1 answer


I think you should be using System.web hosting for your website, so your tests were running. Now that you started using OwinSelf hosting, the HttpContext no longer appears in the picture. This is the reason you are getting null.

It is for this reason that we have extension methods to get OwinContext from HttpContext / Requests, but no extension method to get HttpContext from OwinContext.



Sorry, you need to remove / change the above self-hosting test.

+1


source







All Articles