title: Can I access virtual directory name in global.asax.cs?
body: <p>The property <code>HttpContext.Current.Request.ApplicationPath</code> represents the virtual directory in IIS or WebDev.WebServer.</p>
  
  <pre><code> HttpContext.Current.Request.ApplicationPath evaluates to "/virtualdirectory"
  </code></pre>
  
  <p>This can be used in conjunction with <code>VirtualPathUtility</code> to make a path root relative :</p>
  
  <pre><code> VirtualPathUtility.ToAbsolute("~/images/cat.jpg",
                                 HttpContext.Current.Request.ApplicationPath)
  
   // (this evaluates to "/virtualdirectory/images/cat.jpg")
  </code></pre>
  
  <p>In IIS6 and WebDev.WebServer the Request object <strong>is</strong> available in <code>global.asax.cs</code>, but IIS7 complains that it is 'not available in current context'. Therefore the second line of code above works but not in IIS7.</p>
  
  <p>The problem is I need to access the virtual directroy name within <code>global.asax.cs</code>. I need it to construct some paths that are used in dynamically created CSS. Is there an alternative way to access this value?</p>
  
  <p><strong>Edit:</strong> This is the error you get in IIS 7 for calling <code>HttpContext.Current.Request</code> in global.asax.cs under Application_Start:</p>
  
  <pre><code> HttpException (0x80004005): Request is not available in this context]
      System.Web.HttpContext.get_Request() +8789264
  </code></pre>