3

Currently I'm trying to get the current URL that is shown in the browser.

If I use

Request.Path 

I get https://this.website.com:443/Default.aspx which is technically correct.

However the URL displayed in the browser itself is https://this.website.com/.

Using any of the Request options still will show Default.aspx.

I need to ultimately detect wether or not the url in the browser is https://this.website.com or http://this.website.com/Default.aspx and then redirect to Default.aspx if it's not there.

Btw complicating things more is the https redirect in my web.config.

Peter Rasmussen
  • 16,474
  • 7
  • 46
  • 63
Prescient
  • 1,051
  • 3
  • 17
  • 42
  • Duplicate of [How to get the URL of the current page in C#](http://stackoverflow.com/questions/593709/how-to-get-the-url-of-the-current-page-in-c-sharp) – Daniel B Mar 27 '14 at 20:12
  • 1
    This is not a duplicate question. What I'm trying to figure out is how to distinguish the difference between http://www.site.com/ and http://www.site.com/Default.aspx. Which apparently asp.net determines to be the same. – Prescient Mar 27 '14 at 20:21

1 Answers1

5

You can get it from the request in the httpcontext.

HttpContext.Current.Request.Url

Updated:

If you want to tell wether the current url is / or /default.aspx. You can use the RawUrl property of the request. This field will contain the whole url.

HttpContext.Current.Request.RawUrl
Peter Rasmussen
  • 16,474
  • 7
  • 46
  • 63
  • Nope it brings up https://this.website.com/default.aspx when the url in the browser window is https://this.website.com/ – Prescient Mar 27 '14 at 20:14
  • You'll have to use `HttpContext.Current.Request.Url.Host` – Daniel B Mar 27 '14 at 20:15
  • @DanielB Nope. that only gives me the HOST. I need to find out whether or not the url is this.site.com/ or this.site.com/Default.aspx. – Prescient Mar 27 '14 at 20:18
  • @Prescient then use `HttpContext.Current.Request.Url.AbsolutePath` and make a check? – Daniel B Mar 27 '14 at 20:20
  • nope. what is returned is /Default.aspx and the browser shows http://www.site.com/ – Prescient Mar 27 '14 at 20:23
  • Then maybe check this: [How to tell if a user is visiting “/Default.aspx” or just “/”](http://stackoverflow.com/questions/8629232/how-to-tell-if-a-user-is-visiting-default-aspx-or-just) – Daniel B Mar 27 '14 at 20:27
  • Peter is correct. Request.RawUrl is the way to go. I will be checking Request.RawUrl.Lenth.ToString() for a value less than 2 and redirect if it's found. Thanks. – Prescient Mar 27 '14 at 20:44