Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get raw url from Owin?

Tags:

c#

.net

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).

like image 561
TN. Avatar asked Nov 10 '22 20:11

TN.


1 Answers

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.

like image 85
RobinG Avatar answered Nov 15 '22 04:11

RobinG