0

When I try to get the http://subdomain.domain.com domain's host, the result is always "127.0.0.1". I tried several variations, for example

rootUrl = string.Format("{0}://{1}{2}{3}",
                        context.Request.Url.Scheme,
                        context.Request.Url.Host,
                        context.Request.Url.Port == 80 ? ""
                                                       : ":" + context.Request.Url.Port,
                        context.Request.ApplicationPath);

I also tried other suggestions from this SO page, but the result remains IsLocal=true, Port=80 and Host="127.0.0.1". Why do I not get "subdomain.domain.com" for Host or Request.Url.Authority when on the live web page?

EDIT The same thing happens when accessing the site from different, totally unrelated machines.

Community
  • 1
  • 1
devlock
  • 959
  • 1
  • 9
  • 14

2 Answers2

1

Your code doesn't have problem, looks like the site is accessed either
through visual studio (IIS express) or client (browser) as well as web application is running in same system.

Sudipta Kumar Maiti
  • 1,669
  • 1
  • 12
  • 18
  • the site is accessed through a browser, not via VS; there's no competing application running concurrently on my machine – devlock Oct 20 '15 at 08:45
  • How are you accessing your site? http://subdomain.domain.com or http://localhost/VirtualDirectoryName? If localhost, the behavior is expected. – Sudipta Kumar Maiti Oct 20 '15 at 08:57
  • it's just "subdomain.domain.com", no localhost or so anywhere; as mentioned in the edit above, same thing happens accessing the site from different machines – devlock Oct 20 '15 at 09:05
0

This works for me:

In ASP.NET:

            var ServerBaseUrl = Request.Url.Scheme + "://"
                  + Request.Url.Authority 
                  + (Request.Url.IsDefaultPort ? string.Empty : ":" + Request.Url.Port)
                  + Request.ApplicationPath.TrimEnd('/');

In WebAPI:

            var ServerBaseURL = Request.RequestUri.Scheme + "://"
                            + Request.RequestUri.Authority
                            + (Request.RequestUri.IsDefaultPort ? string.Empty : ":" + Request.RequestUri.Port)
                            + (string.IsNullOrWhiteSpace(Request.GetRequestContext().VirtualPathRoot.Trim('/'))
                                ? string.Empty
                                : (Request.GetRequestContext().VirtualPathRoot));
GeorgDangl
  • 2,146
  • 1
  • 29
  • 37
  • thanks, but Request.RequestUri.Authority also returns "127.0.0.1" :( – devlock Oct 20 '15 at 08:04
  • Do you have a binding to "*" (all domains) on your IIS settings for this site and access it via the server on which the IIS is running? And what URL do you enter in the browser? – GeorgDangl Oct 20 '15 at 08:06
  • there's no particular binding, it's just a visit from my local machine to some subdomain.domain.com, hosted with a major provider; I would expect the web page's root shown in the browser to be returned – devlock Oct 20 '15 at 08:21