Get the exact url the user typed into the browser

后端 未结 6 752
轻奢々
轻奢々 2020-12-05 21:42

I would like to get the exact url that user typed into the browser. Of course I could always use something like Request.Url.ToString() but this does not give me

相关标签:
6条回答
  • 2020-12-05 22:03

    Remember too that the "exact URL that the user entered" may never be available at the server. Each link in the chain from fingers to server can slightly modify the request.

    For example if I type xheo.com into my browser window, IE will be convert to http://www.xheo.com automatically. Then when the request gets to IIS it says to the browser - you really want the default page at http://www.xheo.com/Default.aspx. So the browser responds by asking for the default page.

    Same thing happens with HTTP 30x redirect requests. The server will likely only ever see the final request made by the browser.

    0 讨论(0)
  • 2020-12-05 22:09

    It is possible, you just need to combining a few of the values from the request object to rebuild the exact url entered:

    Dim pageUrl As String = String.Format("{0}://{1}{2}", 
        Request.Url.Scheme, 
        Request.Url.Host, 
        Request.RawUrl)
    
    Response.Write(pageUrl)
    

    Entering the address http://yousite.com/?hello returns exactly:

    http://yousite.com/?hello
    
    0 讨论(0)
  • 2020-12-05 22:15

    Easiest way to do this is used client-side programming to extract the exact url:

    <script language="javascript" type="text/javascript"> 
    document.write (document.location.href); 
    </script>
    
    0 讨论(0)
  • 2020-12-05 22:16
    Request.RawUrl
    

    I think is the monkey you are after...

    0 讨论(0)
  • 2020-12-05 22:17

    Try using Request.Url.OriginalString Might give you the thing you are looking for.

    0 讨论(0)
  • 2020-12-05 22:21

    Edit: You want the HttpWorkerRequest.GetServerVariable() with the key HTTP_URL or CACHE_URL. Note that the behavior differs between IIS 5 and IIS 6 (see documentation of the keys).

    In order to be able to access all server variables (in case you get null), directly access the HttpWorkerRequest:

    HttpWorkerRequest workerRequest = 
      (HttpWorkerRequest)((IServiceProvider)HttpContext.Current)
      .GetService(typeof(HttpWorkerRequest)); 
    
    0 讨论(0)
提交回复
热议问题