我可以访问的global.asax.cs虚拟目录的名称? [英] Can I access virtual directory name in global.asax.cs?

查看:220
本文介绍了我可以访问的global.asax.cs虚拟目录的名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

属性 HttpContext.Current.Request.ApplicationPath 重新presents虚拟目录在IIS或WebDev.WebServer。

  HttpContext.Current.Request.ApplicationPath计算为/ virtualdirectory

这可以结合使用 VirtualPathUtility 来使相对路径根:

  VirtualPathUtility.ToAbsolute(〜/图片/ cat.jpg
                               HttpContext.Current.Request.ApplicationPath) //(此计算结果为/virtualdirectory/images/cat.jpg)

在IIS6和WebDev.WebServer Request对象的提供的global.asax.cs ,但IIS7抱怨说,它是不可用在目前的情况下。因此code的第二线以上的作品,但不是在IIS7。

问题是我需要在的global.asax.cs 访问虚拟directroy名称。我需要它构建在动态创建CSS中使用的一些路径。有没有访问此值的另一种方式?

编辑:这是你在IIS 7中得到调用 HttpContext.Current.Request 中的global.asax.cs的Application_Start下的错误:

  HttpException(0x80004005的):请求不可用在这种情况下]
    System.Web.HttpContext.get_Request()8789264


解决方案

终于找到了答案很简单!

  HttpRuntime.AppDomainAppVirtualPath

中的Application_Start立即可用

这是形式 /所有MyApplication 其中的 / preFIX。

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

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

This can be used in conjunction with VirtualPathUtility to make a 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 current context'. Therefore the second line of code above works but not in IIS7.

The problem is I need to access the virtual directroy name within global.asax.cs. I need it to construct some paths that are used in dynamically created CSS. Is there an alternative way to access this value?

Edit: This is the error you get in IIS 7 for calling HttpContext.Current.Request in global.asax.cs under Application_Start:

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

解决方案

Finally found the simple answer!

 HttpRuntime.AppDomainAppVirtualPath

Available immediately during Application_Start

This is of the form /myapplication including the / prefix.

这篇关于我可以访问的global.asax.cs虚拟目录的名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆