HttpContext.Current.Request.Url.Host what it returns?

120,800

Solution 1

Yes, as long as the url you type into the browser www.someshopping.com and you aren't using url rewriting then

string currentURL = HttpContext.Current.Request.Url.Host;

will return www.someshopping.com

Note the difference between a local debugging environment and a production environment

Solution 2

The Host property will return the domain name you used when accessing the site. So, in your development environment, since you're requesting

http://localhost:950/m/pages/Searchresults.aspx?search=knife&filter=kitchen

It's returning localhost. You can break apart your URL like so:

Protocol: http
Host: localhost
Port: 950
PathAndQuery: /m/pages/SearchResults.aspx?search=knight&filter=kitchen

Solution 3

Try this:

string callbackurl = Request.Url.Host != "localhost" 
    ? Request.Url.Host : Request.Url.Authority;

This will work for local as well as production environment. Because the local uses url with port no that is possible using Url.Host.

Share:
120,800

Related videos on Youtube

Amin Sayed
Author by

Amin Sayed

Updated on July 09, 2022

Comments

  • Amin Sayed
    Amin Sayed almost 2 years

    I have a local application which has a path:

    http://localhost:950/m/pages/Searchresults.aspx?search=knife&filter=kitchen
    

    but when this goes to integration environment or perhaps the production, it will be something like

    http://www.someshopping.com/m/pages/SearchResults.aspx?search=knife&filter=kitchen
    

    For some cases I need to pass just:

    www.someshopping.com
    

    to my XSLT file and in one of the function I'm using this:

    string currentURL = HttpContext.Current.Request.Url.Host;
    

    this returns me "localhost" in local environment. Will the same code return me:

    www.someshopping.com in production (I DO NOT need http://)

    just don't want to take any chance. So asked this silly question.

    • Spike0xff
      Spike0xff about 10 years
      maybe should be string host = HttpContext.Current.Request.Url.Host;
  • Amin Sayed
    Amin Sayed over 11 years
    So my question goes will this return www.someshopping.com in production environment ? :)
  • Tejs
    Tejs over 11 years
    Yes it will, assuming the URL you are requesting in production is www.someshopping.com.
  • balexandre
    balexandre over 8 years
    you should always use Request.IsLocal to check if it's a local request, no need to compare the Request.Url.Host as that's false if I actually write http://LocalHost/...
  • Alex
    Alex over 6 years
    It returns 'HOSTNAME' header or local address if the header is not present. See sources for details: referencesource.microsoft.com/#System.Web/…