我可以访问global.asax.cs中的虚拟目录名称吗?
属性 HttpContext.Current.Request.ApplicationPath
表示 IIS 或 WebDev.WebServer 中的虚拟目录。
HttpContext.Current.Request.ApplicationPath evaluates to "/virtualdirectory"
这可以与 VirtualPathUtility 结合使用来使路径根相对:
VirtualPathUtility.ToAbsolute("~/images/cat.jpg",
HttpContext.Current.Request.ApplicationPath)
// (this evaluates to "/virtualdirectory/images/cat.jpg")
在 IIS6 和 WebDev.WebServer 中,Request 对象在 global.asax.cs< 中
可用。 /code>,但 IIS7 抱怨它“在当前上下文中不可用”。 因此上面的第二行代码可以工作,但在 IIS7 中不行。
问题是我需要访问 global.asax.cs
中的虚拟目录名称。 我需要它来构造一些在动态创建的 CSS 中使用的路径。 有没有其他方法可以访问这个值?
编辑:这是在 IIS 7 中调用 Application_Start 下的 global.asax.cs 中的 HttpContext.Current.Request
时出现的错误:
HttpException (0x80004005): Request is not available in this context]
System.Web.HttpContext.get_Request() +8789264
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
嗯...我不知道 IIS7 的变化。 我想知道推迟此操作是否会更简单,直到您已经获得一个页面。 例如,您可以尝试在
Application_BeginRequest
或Session_Start
中添加“仅一次”的内容?或者(完全未经测试)对于自取消订阅挂钩:
技巧是只执行一次(如果多个请求同时到达); 也许是静态演员? 例如,我认为这只触发一次,并且仅当上下文中存在可用页面时:
Hmmm... I wasn't aware of the IIS7 change. I wonder if it wouldn't be simpler to defer this operation until you have got a page. For example, you could try putting something "once only" in
Application_BeginRequest
orSession_Start
?Or (completely untested) for a self-unsubscribing hook:
The trick is doing it once only (if multiple requests arrive at once); perhaps a static ctor? For example, I think this fires once only, and only when there is a page available in context:
这是我想出的最好的:Application_BeginRequest(通过标记)
我很少使用asax,以至于我暂时忘记了你会用它得到不同的事件。 到目前为止,我一直在
Application_Start
中创建 CSS 精灵。 将其移至 BeginRequest 是我能想到的最好的办法。对每个请求进行一次布尔检查可以忽略不计,但如果有不同的方法那就太好了。
This is the best I came up with : Application_BeginRequest (via mark)
I use asax so rarely that I had temporarily forgotten you get different events with it. Until now I'd been creating the CSS sprites in
Application_Start
. Moving it to BeginRequest was the best I could come up with.One boolean check for every request is negligible, but would be nice if there is a different way.
当切换到 IIS7 时我也遇到了这个问题,但我能够重构对 Request 的需求。 这也是这个人的建议,如果你不能的话,还提供了一个解决方法。
http://mvolo.com/blogs/serverside/archive/2007/11/10/Integrated-mode-Request-is-not-available-in-this-context-in-Application_5F00_Start.aspx< /a>
I had this problem too when switching to IIS7 but I was able to refactor out the need for Request. Which is what this guy also suggests and provides a workaround if you can't.
http://mvolo.com/blogs/serverside/archive/2007/11/10/Integrated-mode-Request-is-not-available-in-this-context-in-Application_5F00_Start.aspx
终于找到简单的答案了!
在 Application_Start 期间立即可用
它的格式为
/myapplication
,包括/
前缀。Finally found the simple answer!
Available immediately during Application_Start
This is of the form
/myapplication
including the/
prefix.您可以使用 ResolveUrl("~/images/cat.jpg") 来构建路径吗?
编辑: ResolveUrl 是 Control 的一种方法,而不仅仅是 Page 类,因此您可以这样做(可能有点难看):
Can you use ResolveUrl("~/images/cat.jpg") to build your path?
Edit: ResolveUrl is a method of Control, not just the Page class, so you can do it this way instead (bit ugly maybe):