如何在ASP.NET MVC中声明全局变量?
答案 0 :(得分:78)
从技术上讲,项目中任何地方的任何静态变量或属性都是全局变量,例如。
public static class MyGlobalVariables
{
public static string MyGlobalString { get; set; }
}
但正如@SLaks所说,如果处理不当,他们可能“潜在地”成为不好的做法并且很危险。例如,在上面的示例中,您将有多个请求(线程)尝试访问同一个Property,如果它是复杂类型或集合,则可能是一个问题,您必须实现某种形式的锁定。
答案 1 :(得分:44)
public static class GlobalVariables
{
// readonly variable
public static string Foo
{
get
{
return "foo";
}
}
// read-write variable
public static string Bar
{
get
{
return HttpContext.Current.Application["Bar"] as string;
}
set
{
HttpContext.Current.Application["Bar"] = value;
}
}
}
答案 2 :(得分:24)
您可以将它们放入应用程序:
Application["GlobalVar"] = 1234;
它们仅在当前IIS / Virtual应用程序中是全局的。这意味着,在webfarm上,它们是服务器的本地,并且位于作为应用程序根目录的虚拟目录中。
答案 3 :(得分:18)
对于非静态变量,我通过应用程序类字典将其整理出来,如下所示:
在Global.asax.ac:
namespace MvcWebApplication
{
// Note: For instructions on enabling IIS6 or IIS7 classic mode,
// visit http://go.microsoft.com/?LinkId=9394801
public class MvcApplication : System.Web.HttpApplication
{
private string _licensefile; // the global private variable
internal string LicenseFile // the global controlled variable
{
get
{
if (String.IsNullOrEmpty(_licensefile))
{
string tempMylFile = Path.Combine(Path.GetDirectoryName(Assembly.GetAssembly(typeof(LDLL.License)).Location), "License.l");
if (!File.Exists(tempMylFile))
File.Copy(Server.MapPath("~/Content/license/License.l"),
tempMylFile,
true);
_licensefile = tempMylFile;
}
return _licensefile;
}
}
protected void Application_Start()
{
Application["LicenseFile"] = LicenseFile;// the global variable's bed
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
}
}
在控制器中:
namespace MvcWebApplication.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
return View(HttpContext.Application["LicenseFile"] as string);
}
}
}
通过这种方式,我们可以在ASP.NET MVC中拥有全局变量:)
注意:如果您的对象不是字符串,只需写:
return View(HttpContext.Application["X"] as yourType);
答案 4 :(得分:8)
您还可以使用静态类,例如Config类或其他类似的东西......
public static class Config
{
public static readonly string SomeValue = "blah";
}
答案 5 :(得分:0)
钢铁远非热,但我将@ abatishchev的解决方案与this post的答案结合起来并得出了这个结果。希望它有用:
public static class GlobalVars
{
private const string GlobalKey = "AllMyVars";
static GlobalVars()
{
Hashtable table = HttpContext.Current.Application[GlobalKey] as Hashtable;
if (table == null)
{
table = new Hashtable();
HttpContext.Current.Application[GlobalKey] = table;
}
}
public static Hashtable Vars
{
get { return HttpContext.Current.Application[GlobalKey] as Hashtable; }
}
public static IEnumerable<SomeClass> SomeCollection
{
get { return GetVar("SomeCollection") as IEnumerable<SomeClass>; }
set { WriteVar("SomeCollection", value); }
}
internal static DateTime SomeDate
{
get { return (DateTime)GetVar("SomeDate"); }
set { WriteVar("SomeDate", value); }
}
private static object GetVar(string varName)
{
if (Vars.ContainsKey(varName))
{
return Vars[varName];
}
return null;
}
private static void WriteVar(string varName, object value)
{
if (value == null)
{
if (Vars.ContainsKey(varName))
{
Vars.Remove(varName);
}
return;
}
if (Vars[varName] == null)
{
Vars.Add(varName, value);
}
else
{
Vars[varName] = value;
}
}
}