18

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

BWA
  • 5,672
  • 7
  • 34
  • 45
Paddy
  • 33,309
  • 15
  • 79
  • 114

6 Answers6

24

You can access the Context through the static HttpContext.Current member.

HttpContext.Current.Request.ServerVariables["SERVER_NAME"];
HttpContext.Current.Request.Url;

Edit, Based on some of your comments I did some additional research

This error is due to a design change in the IIS7 Integrated pipeline that makes the request context unavailable in Application_Start event. When using the Classic mode (the only mode when running on previous versions of IIS), the request context used to be available, even though the Application_Start event has always been intended as a global and request-agnostic event in the application lifetime. Despite this, because ASP.NET applications were always started by the first request to the app, it used to be possible to get to the request context through the static HttpContext.Current field.

So you have two options

  1. Change your application code to not use the request context (recommended).
  2. Move the application to Classic mode (NOT recommended).

http://mvolo.com/iis7-integrated-mode-request-is-not-available-in-this-context-exception-in-applicationstart/

jacroe
  • 106
  • 10
Bob
  • 97,670
  • 29
  • 122
  • 130
  • 3
    I don't think there is a HttpContext in Application_Start – Luk Nov 24 '09 at 14:42
  • 2
    @Luk - HttpContext.Current is a static member. It doesn't matter what scope you are in. It is there. If you are having trouble with it, fully qualify it System.Web.HttpContext.Current – Bob Nov 24 '09 at 14:59
  • 5
    If I do this, I get a runtime error stating that Request is not available in this context. Qualification makes no difference. – Paddy Nov 24 '09 at 15:29
  • @Bob - please see the link I just posted in the question. – Paddy Nov 24 '09 at 15:37
  • 1
    What if HttpContext.Current.Request is still null? – Stuart Dobson Mar 25 '15 at 03:40
  • Any idea why HttpContext.Current throws an exception when running on my local machines IIS instance but when I deploy it on my web server it does not throw an exception? – James Wierzba Sep 28 '16 at 16:01
18

Your web-application could run under multiple different domains. Since there is no current request in the Application_Start event, you cannot know under which domain the application will be called.

You could however find out the machine-name using System.Environment.MachineName.

Vinz
  • 2,010
  • 1
  • 13
  • 16
5

I'm guessing you are on IIS 7? Because the HttpContext is available there on IIS 6.0.

Can you consider filling that information later on? The first call to Application_BeginRequest for example?

Luk
  • 5,371
  • 4
  • 40
  • 55
  • I am on IIS7, how interesting... I'm currently using Application_BeginRequest, but I'd rather have this code that I need to run once in the correct event. – Paddy Nov 24 '09 at 15:30
  • @Downvoter: where am I wrong? I'm pretty sure you can't access it before BeginRequest in IIS7 – Luk Nov 24 '09 at 15:34
  • Have an up to cancel that out... You would seem to be the right answer. – Paddy Nov 24 '09 at 15:36
4

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
Matt Roy
  • 1,455
  • 1
  • 17
  • 27
0

Do you have access to the Request object at all? If so i think you could use Request.Url.Authority

It will return the dns host name which is what you are looking for.

G

gg.
  • 658
  • 3
  • 14
-3

Have you tried: -

System.Web.HttpContext.Current.Request.ServerVariables["SERVER_NAME"]

Thanks,

Phil.

Plip
  • 1,040
  • 8
  • 9
  • +1 Surprisingly this works even in `Application_Start` where `HttpContext.Current.Request` is `null`. It turns out that `ServerVariables` is a static (aka global) variable. – Andomar Jan 04 '11 at 15:32
  • 5
    -1: Under the circumstances of the Q (IIS>=7 & Integrated mode) this will fail in the same way. – Richard May 11 '11 at 16:55