2

I need to access the website url in Application_Start event. Since HttpRequest in context will not be valid, I am unable to retrieve URL. Are there any alternative to retrieve URL of the application?

For example, I need to fetch https://somehost/root/, assuming the app is hosted under root in IIS.

Thanks In Advance

Lokeshwer
  • 1,139
  • 1
  • 9
  • 14

1 Answers1

1

Try Application_AcquireRequestState:

  void Application_AcquireRequestState(object sender, EventArgs e)
  {
      string url = Request.Url.ToString();
  }
Lynn Crumbling
  • 12,985
  • 8
  • 57
  • 95
  • Thanks. But Application_AcquireRequestState is fired per request. I need it to be called only once so I was wondering if there are other ways – Lokeshwer Apr 18 '12 at 05:57
  • But how would IIS know, at app start time, what the corresponding URL is for the virtual directory? You could have several host headers defined, all pointing to the same virt dir. IIS wouldn't know what the actual URL is until a request comes in. To get around the problem of it only being called once, you could perhaps initialize a global string to string.empty, only set it if it's not string.empty.... else return; – Lynn Crumbling Apr 18 '12 at 14:18