Global ASAX - get the server name

Viewed 44907

Can anybody tell me if there is a way for me to get the domain name of my site in the Application_Start event in the global.asax?

Normally I'd just get it from Context.Request.ServerVariables["SERVER_NAME"], but this is not available. I'd ideally also like to get the URL from the request that kicked off the application.


Hmm - from answers below, it would seem that being on IIS7 makes a difference here. This is new and there are now design guidelines to try and stop you from doing it:

IIS Blog

6 Answers

In VB.NET, in Global.asax, I use the following:

Hosting.HostingEnvironment.ApplicationHost.GetSiteName

It corresponds to the application name in IIS.

UPDATE: It seems the method "GetSiteName" is not intended to be called directly and it doesn't work anymore for me in Visual Studio 2015 (or maybe it is because the framework version change I made). I fixed it by replacing it by:

System.Web.Hosting.HostingEnvironment.SiteName
Related