惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

L
LangChain Blog
V
V2EX
爱范儿
爱范儿
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Martin Fowler
Martin Fowler
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Apple Machine Learning Research
Apple Machine Learning Research
WordPress大学
WordPress大学
有赞技术团队
有赞技术团队
宝玉的分享
宝玉的分享
Last Week in AI
Last Week in AI
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
罗磊的独立博客
小众软件
小众软件
Vercel News
Vercel News
博客园 - 司徒正美
阮一峰的网络日志
阮一峰的网络日志
V
Visual Studio Blog
J
Java Code Geeks
P
Proofpoint News Feed
MongoDB | Blog
MongoDB | Blog
B
Blog
美团技术团队
量子位

博客园 - Tam

ASP.NET 验证控件 log4net - 4步快速配置跟踪日志(ASP.NET) Windows Workflow Foundation - [Workflow Runtime] Windows Workflow Foundation - [介绍及相关资源] TortoiseSVN 安装使用 VS2005里三种有用的调试方法 使用Iframe时要注意的一个问题 - Tam - 博客园 必须要知道的GridView用法(1) 试用Code Snippets Manager 学习使用ASP.NET 2.0的本地化 XSLT 运算符 和 记录一些用过的语句 IIS里面设置好虚拟目录后访问出现Server Application Unavailable 转载:如何在自己的网站上做天气预报? - Tam - 博客园 Linux Shell Record 在VMWARE上安装Fedora Core 5遇到的错误 DCOM错误 TASTE CSS DESIGN ASP.NET 传递参数到XSLT - Tam - 博客园 ASP.NET中如何知道文件是否被下载过?
IIS 7.0集成模式中Application_Start不能使用HttpContext.Cur...
Tam · 2009-09-21 · via 博客园 - Tam

2009-09-21 22:57  Tam  阅读(1129)  评论()    收藏  举报

在IIS7集成模式中,当我们在Global.asax的Application_Start中使用HttpContext.Current.Request.ApplicationPath获得当前虚拟目录路径,会得到以下的错误:

Server Error in '/IIS' Application.

Request is not available in this context

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: Request is not available in this context

Source Error:

Line 6: { Line 7: // Code that runs on application startup Line 8: var appPath = HttpContext.Current.Request.ApplicationPath; Line 9: } Line 10: 

根据ASP.NET 2.0 Breaking Changes on IIS 7.0中的第十六条,我们可以清楚看到Application_Start事件中已不能使用HttpContext.Current。

如果我们只是需要获取当前的虚拟目录名,可以使用HttpRuntime.AppDomainAppVirtualPath代替。