Can I access the virtual directory name in global.asax.cs?

The HttpContext.Current.Request.ApplicationPath property represents a virtual directory in IIS or WebDev.WebServer.

  HttpContext.Current.Request.ApplicationPath evaluates to "/virtualdirectory" 

This can be used in conjunction with VirtualPathUtility to make the path root relative:

  VirtualPathUtility.ToAbsolute("~/images/cat.jpg", HttpContext.Current.Request.ApplicationPath) // (this evaluates to "/virtualdirectory/images/cat.jpg") 

In IIS6 and WebDev.WebServer, the Request object is available in global.asax.cs , but IIS7 complains that it is "not available in the current context." Therefore, the second line of code above works, but not in IIS7.

The problem is that I need to access the virtual directroy name inside global.asax.cs . I need him to build several paths that are used in dynamically generated CSS. Is there an alternative way to access this value?

Edit: This is the error you get in IIS 7 to call HttpContext.Current.Request in global.asax.cs in Application_Start:

  HttpException (0x80004005): Request is not available in this context] System.Web.HttpContext.get_Request() +8789264 
+7
source share
5 answers

Finally found a simple answer!

  HttpRuntime.AppDomainAppVirtualPath 

Available immediately during Application_Start

This has the form /myapplication , including the / prefix.

+13
source share

Can you use ResolveUrl ("~ / images / cat.jpg") to create your own path?

Edit: ResolveUrl is a management method, not just a page class, so you can do it instead (perhaps ugly):

 System.Web.UI.Control c = new Control(); String s = c.ResolveUrl(@"~/images/cat.jpg"); 
0
source share

Hmmm ... I did not know the IIS7 change. I wonder if it would be easier to postpone this operation until you have a page. For example, can you try putting something "only once" in Application_BeginRequest or Session_Start ?

Or (completely untested) for a self-destructing hook:

  public override void Init() { base.Init(); EventHandler handler = null; handler = delegate { // do stuff, once only this.BeginRequest -= handler; }; this.BeginRequest += handler; } 

The trick does this only once (if multiple requests arrive at once); maybe static ctor? For example, I think this only works once and only when there is a page in the context:

  static class DelayedLoader { static DelayedLoader() { string s = VirtualPathUtility.ToAbsolute("~/images/cat.jpg", HttpContext.Current.Request.ApplicationPath); } [MethodImpl(MethodImplOptions.NoInlining)] public static void Init() { } } public override void Init() { base.Init(); EventHandler handler = null; handler = delegate { DelayedLoader.Init(); this.BeginRequest -= handler; }; this.BeginRequest += handler; } 
0
source share

This is the best I came up with: Application_BeginRequest (via checkmark)

I use asax so rarely that I temporarily forgot that different events are happening to it. So far, I have been creating CSS sprites in Application_Start . Moving it to BeginRequest was the best I could come up with.

One logical check for each request is negligible, but it would be nice if there was a different way.

  protected void Application_Start() { RegisterRoutes(RouteTable.Routes); } protected void Application_BeginRequest() { if (!_initialized) { lock (thisLock) { _initialized = true; GenerateCSSSprites(); } } } 
0
source share

I also had this problem when switching to IIS7, but I was able to reorganize the need for the request. This is what this guy offers and offers a workaround if you cannot.

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

0
source share

All Articles