8

We currently use this line of code to get the current applications url in the Application_Start event.

string sApplicationURL = HttpContext.Current.Request.Url.Scheme + "://" 
                         + HttpContext.Current.Request.Url.Authority 
                         + HttpContext.Current.Request.ApplicationPath;

I just recently found out that in IIS7.0 the Request object is no longer available when the Application_Start event is fired.

Is there another way to get the current applications url without using the Request object?

Thanks

desi
  • 793
  • 2
  • 7
  • 8

2 Answers2

11

Have a look at this: http://mvolo.com/blogs/serverside/archive/2007/11/10/Integrated-mode-Request-is-not-available-in-this-context-in-Application_5F00_Start.aspx

In summary, the error occurs because the Request context is not longer available to the Application_Start event. This blog states two choices to deal with this error:

1) Change your code to work w/o Request, or 2) Modify your application to run in Classic Mode (not recommended).

To get the ApplicationPath, use HttpRuntime.AppDomainAppVirtualPath.

demoncodemonkey
  • 11,730
  • 10
  • 61
  • 103
Jess
  • 2,991
  • 3
  • 27
  • 40
  • 4
    you should elaborate instead of posting a bare link to a blog which could be moved – onof Apr 21 '11 at 21:51
  • 1
    +1. In addition you can get the other two pieces of information, by implementing a handler for the Application_BeginRequest event. This is described in the article in the answer. – Naraen Apr 22 '11 at 00:45
0

Microsoft has a all-in-one article on all breaking changes including this one,

http://learn.iis.net/page.aspx/381/aspnet-20-breaking-changes-on-iis-70/

Yes, its content somehow comes from http://mvolo.com (and the reason is simple).

Lex Li
  • 60,503
  • 9
  • 116
  • 147
  • 2
    All this is useful but is there a way to get the application URL in the Application_Start event? – desi Apr 25 '11 at 15:37
  • Actually no. An ASP.NET website can have multiple bindings, it could run under multiple hosts and multiple port numbers https://stackoverflow.com/questions/4243270/how-to-get-full-host-name-port-number-in-application-start-of-global-aspx – frenchone Oct 04 '17 at 08:23