11

Normally to access the current domain name e.g where the site is hosted I do something like

string rURL = HttpContext.Current.Request.Url.ToString().ToLower();

But HttpContext is not avaible on Application_Start only from Application_BeginRequest.

Any ideas?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
TheAlbear
  • 5,507
  • 8
  • 51
  • 84
  • 1
    Sort of related? http://stackoverflow.com/questions/2180905/how-do-i-get-the-host-domain-name-in-asp-net-without-using-httpcontext-current-r – Doozer Blake Oct 04 '11 at 12:10
  • 1
    possible duplicate of [How to get full host name + port number in Application_Start of Global.aspx?](http://stackoverflow.com/questions/4243270/how-to-get-full-host-name-port-number-in-application-start-of-global-aspx) – Bala R Oct 04 '11 at 12:12
  • Yep didn't see that when looking and it wasn't flagged when I typed the question. flagged for deletion. – TheAlbear Oct 04 '11 at 12:16
  • You could also try [`System.Web.Hosting.HostingEnvironment.SiteName`](https://msdn.microsoft.com/en-us/library/system.web.hosting.hostingenvironment.sitename(v=vs.110).aspx). It's not strictly the domain name, but is the IIS site name. – Dan Atkinson Feb 13 '18 at 14:41

4 Answers4

8

A single IIS application can be bound to many different URLs. Application_Start fires before any request has been received, so the only way to find the domain name to which the site is bound is to query IIS.

Even then you may not be able to get an answer - consider the situation where an application is bound to a wildcard / default hostname.

A better approach may be to look at Application_AuthenticateRequest. This fires before Application_BeginRequest and does give you a full HttpContext.Current.Request object.

Jeremy McGee
  • 24,842
  • 10
  • 63
  • 95
  • Thought it might be something like this, putting the URL to access in the web.config I think is the best way forward. – TheAlbear Oct 04 '11 at 12:11
  • 3
    `Application_Start` only fires when the application - eh - starts. :) Whereas `Application_AuthenticateRequest` fires on every request. This doesn't answer the question accurately. – hofnarwillie Feb 18 '13 at 17:38
7

Try Dns.GetHostName().

You can use this in Gloabl.asax.cs, no matter within Application_Start() or not.

var hostName = Dns.GetHostName();

tested in Asp.net 4.5 MVC.

Dan Atkinson
  • 11,391
  • 14
  • 81
  • 114
Chieh
  • 128
  • 1
  • 2
  • 2
    Note that this may not always work as expected. For example, on my local computer I have a hosts entry for a domain (e.g. example.com), and also have bindings configured in IIS. When using this method, however, I get the computer name and not the domain name. In this scenario I get something like "vm-dev1". – trnelson Apr 21 '17 at 14:57
3

One way of achieving this is a little bit of a cheat and comes with caveats, and that's to use System.Web.Hosting.HostingEnvironment.SiteName.

So this would look like:

string rURL = System.Web.Hosting.HostingEnvironment.SiteName;

And now for those caveats:

  • This is the name of the site in IIS.
  • That means your site name can be a non-URI (eg My Awesome Site).
  • If you have multiple domains sharing the same site, it'll be the same for each one.

For my purposes - setting up logging on servers where I had multiple sites in IIS pointing to the same physical folder - the above solution was probably the simplest and easiest. I'm not saying it's necessarily the 'right' answer, but it should be considered as an alternative.

Dan Atkinson
  • 11,391
  • 14
  • 81
  • 114
3

The IIS application does not know what domain its accessed from (see bindings) at application start.

Jonas B
  • 2,351
  • 2
  • 18
  • 26