简体   繁体   English

如何访问HttpContext.Current.Application

[英]how to access HttpContext.Current.Application

Hello i have problem accessing HttpContext.Current.Application From global.asax its seems to be null every time i try to access it. 您好,我在从global.asax访问HttpContext.Current.Application时遇到问题,每次我尝试访问它时,它似乎都为空。 How can i to this? 我该怎么办?

HttpContext.Current.Application.Lock();
HttpContext.Current.Application["Actions"] = "hello";
HttpContext.Current.Application.UnLock();

Thanks 谢谢

In Global.asax, you're not in an actual request, so there's no current request nor context you can go by. 在Global.asax中,您不在实际请求中,因此没有当前请求,也没有上下文可以通过。 However, the Global class is a subclass of HttpApplication, so just use 'this', like: 但是,Global类是HttpApplication的子类,因此只需使用“ this”,例如:

this["Actions"]

You need to turn on access to the Application instance object in your web.config file like so: 您需要像这样在web.config文件中打开对Application实例对象的访问权限:

<system.serviceModel>
  <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
</system.serviceModel>

Be sure to include a reference to System.Web. 确保包含对System.Web的引用。

From MSDN MSDN

To get the HttpApplication object for the current HTTP request, use ApplicationInstance. 要获取当前HTTP请求的HttpApplication对象,请使用ApplicationInstance。 (ASP.NET uses ApplicationInstance instead of Application as a property name to refer to the current HttpApplication instance in order to avoid confusion between ASP.NET and classic ASP. In classic ASP, Application refers to the global application state dictionary.) (ASP.NET使用ApplicationInstance而不是Application作为属性名称来引用当前的HttpApplication实例,以避免ASP.NET和经典ASP之间的混淆。在经典ASP中,Application指的是全局应用程序状态字典。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM