Accessing HttpApplication.Application variables from a class

Tes*_*123 8 c# asp.net caching session-variables

I set up various global parameters in Global.asax, as such:

Application["PagePolicies"] = "~/Lab/Policies.aspx";
Application["PageShare"] = "/Share.aspx";
Application["FileSearchQueries"] = Server.MapPath("~/Resources/SearchQueries.xml");
...
Run Code Online (Sandbox Code Playgroud)

I have no problem accessing these variables form .ascx.cs or .aspx.cs file -- ie. files that are part of the Web content. However, I can't seem to access 'Application' from basic class objects (ie. standalone .cs files). I read somewhere to use a slight variations in .cs files, as follows, but it always comes throws an exception when in use:

String file = (String)System.Web.HttpContext.Current.Application["FileSearchQueries"];
Run Code Online (Sandbox Code Playgroud)

EMP*_*EMP 7

虽然您可以HttpContext.Current在任何类中使用,但在调用它时仍必须处理HTTP请求 - 否则就没有当前上下文.我认为这是你获得例外的原因,但发布实际例外将有助于澄清问题.