7

Inside the Application_Start of my Global.asax.cs, I am trying to get the current application path using:

var virtualPath = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority)
                          + HttpRuntime.AppDomainAppVirtualPath;

This will return for example: http://localhost:99/MySite/

I will then use this URL and do the following:

var pageToHit = virtualPath + Pages\MyOtherPage.aspx 
var client = new WebClient();
client.DownloadData(dummyPageUrl);

All this is fine when I run the project in IIS 6 or the Visual Studio built-in web server, however things go crazy in IIS 7 as I get a "System.Web.HttpException: Request is not available in this context".

I am aware of this thread: Request is not available in this context

However, I was wondering if anyone had any idea on how to do the above without changing the project to run in classic mode.

Community
  • 1
  • 1
MaYaN
  • 6,683
  • 12
  • 57
  • 109
  • Also, this appears to be a duplicate of the How to get full host name + port number in Application_Start of Global.aspx? question: http://stackoverflow.com/questions/4243270/how-to-get-full-host-name-port-number-in-application-start-of-global-aspx – JamieSee May 15 '12 at 16:21

1 Answers1

5

You cannot access the absolute url of the current request inside Application_Start when running in integrated mode. You could access the virtual path name using HostingEnvironment.ApplicationVirtualPath but not an absolute url. Here's an article which explains a common workaround. As explained in the article you have 2 possibilities:

  1. Change your application code to not use the request context (recommended)
  2. Perform the initialization in Application_BeginRequest using a lock and a singleton to ensure that this initialization is performed only once for the entire lifetime of the AppDomain. Here's a similar thread discussing this second approach.
Community
  • 1
  • 1
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • ok is there a way of serving a page manually? for example var client = new WebClient(); client.DownloadData(Pages\DummyPage.aspx) – MaYaN May 15 '12 at 15:58
  • No, the WebClient method requires an absolute url. – Darin Dimitrov May 15 '12 at 16:04
  • I see. now the problem in Application_BeginRequest is that once the application is started the _BeginRequest event is not fired until a user lands on any of the pages. That is a problem as I need to hit the page as soon as the application is started. – MaYaN May 15 '12 at 16:08
  • 1
    But you can't *hit* the page in `Application_Start` because you don't know the address of this page at that stage. Also your application is currently being initialized and you are attempting to trigger requests to it. Seems a wrong approach to me. What are you trying to achieve? Why do you need to hit this page in Application_Start? – Darin Dimitrov May 15 '12 at 16:26
  • Correct, but surely there should be a way as this is prefectly possible in iis6 unless it can't be done due to a major architecture change. – MaYaN May 15 '12 at 16:27
  • Yes, it can't be done because there's a major architecture change in IIS7. It's called integrated pipeline mode. You could always switch back to the Classic mode in IIS 7 but honestly you are loosing all the benefits if you do so. – Darin Dimitrov May 15 '12 at 16:28
  • You are right, I will try to see if I can use the _BeginRequest method you suggested. Thanks for your prompt answers. – MaYaN May 15 '12 at 16:43