TN.
TN.

Reputation: 19820

How to get raw url from Owin?

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

For instance, both http://localhost/myapp and http://localhost/myapp/ contains in IOwinRequest.Path the /. The PathBase contains always /myapp and Uri.OriginalString contains always http://localhost/myapp/.

(In ASP.NET I would call HttpContext.Current.Request.RawUrl which returns either /myapp or /myapp/.)

Reason: Currently, I need it to do the server side redirect to add the trailing / if it is missing (independently on the hosting).

Upvotes: 5

Views: 3170

Answers (1)

RobinG
RobinG

Reputation: 399

You can get the raw Url in Owin by accessing 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 recover things like a trailing / in the Url, but will also get the Url string before any decoding is applied, so you can distinguish between '!' and %21, for instance.

Upvotes: 1

Related Questions