det*_*ale 18

例如,在global.asax中:

Application["AppVar"] = "hello";
Run Code Online (Sandbox Code Playgroud)

在任何控制器方法中:

string appVar = HttpContext.Application["AppVar"] as string;
Run Code Online (Sandbox Code Playgroud)

更新(7/2018):
如果需要从DLL库访问MVC全局应用程序数据:

using System.Web;
....
if (HttpContext.Current != null && HttpContext.Current.Application != null)
    string appVar = HttpContext.Current.Application["AppVar"] as string;
Run Code Online (Sandbox Code Playgroud)

检查HttpContext.Current.Application对null也更安全,因为一些假的httpcontext库(在单元测试项目中使用)可能具有null"Application"的有效上下文.