2012-05-28 157 views
1

我想将我的MVC 3应用程序设置加载到内存中以获得更好的性能。访问控制器中的Global.asa.cs变量

到目前为止,我可以在global.asa.cs中指定它们,但我无法访问任何控制器中的变量。任何想法为什么?

Global.asa.cs代码

public class MvcApplication : System.Web.HttpApplication 
{ 
    public static string DBUserName = Properties.Settings.Default.DBUserName; 
    public static string DBPassword = Properties.Settings.Default.DBPassword; 

HomeController的代码:

public class HomeController : Controller 
    { 


     public ActionResult Index() 
     { 
      ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application."; 
      _Authenticate(DBUserName , DBPassword); 

回答

3

您需要通过指定在声明它们的类名来访问它们:

_Authenticate(MvcApplication.DBUserName, MvcApplication.DBPassword); 

MvcApplication是在这两个fields宣布的类的名称。

对于这个工作,你需要为静态申报2场:

public static string DBUserName = Properties.Settings.Default.DBUserName; 
public static string DBPassword = Properties.Settings.Default.DBPassword; 
+0

我收到以下错误:一个对象引用需要非静态字段,方法或属性 – MataHari

+0

不好意思啊,您需要将这些字段声明为静态。我会更新我的答案。 –

+0

你大伙....... – MataHari

相关问题