2012-05-28 259 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

相關問題