How to get raw URL from Owin?

How can I get a raw URL from Owin (the URL that was passed to the HTTP request), independently on Owin hosting?

For example, http: // localhost / myapp and http: // localhost / myapp / contains in IOwinRequest.Path /. PathBasealways contains /myapp, but Uri.OriginalStringalways contains http: // localhost / myapp / .

(In ASP.NET, I would say HttpContext.Current.Request.RawUrlthat returns either /myappor /myapp/.)

Reason: Currently, I need to do a server-side redirection to add the final /one if it is missing (regardless of the hosting).

+4
source share
1 answer

You can get the raw URL in Owin by referring to the HttpListenerContext that was used to receive the request.

    public static string RealUrlFromOwin(HttpRequestMessage request)
    {
        var owincontext = ((OwinContext) request.Properties["MS_OwinContext"]);
        var env = owincontext.Environment;
        var listenerContext = (System.Net.HttpListenerContext) env["System.Net.HttpListenerContext"];
        return listenerContext.Request.RawUrl;
    }

This will not only restore things like trailing / in Url, but also get the Url string before any decoding is applied so that you can distinguish between '!' and% 21, for example.

+1
source

All Articles