2009-05-29 42 views
0

我一直在我的Web表單代碼隱藏類中使用以下代碼。我想在我的常規.cs文件中使用它:例如,在進行數據庫調用時。但是,當我試圖在Web表單外部使用它時,在嘗試映射base.Request.ApplicationPath時出現以下錯誤:'object'不包含'Request'的定義。映射到Web應用程序項目中的常規.cs類文件中的根的簡單而正確的方法是什麼?如何在ASP.NET非表格類文件中映射路徑

protected void LogError(string errMsg, string errTrace) 
    { 
     string mailSubject = "oops!"; 
     string mailBody = errMsg + errTrace; 

     System.Configuration.Configuration config 
      = WebConfigurationManager.OpenWebConfiguration(base.Request.ApplicationPath); 
     AppSettingsSection appSettings = (AppSettingsSection)config.GetSection("appSettings"); 

     // Extract values from the web.config file 
     string toAddress = "[email protected]"; 
     string fromAddress = appSettings.Settings["FromEmailAddr"].Value; 
     string emailHost = appSettings.Settings["EmailHost"].Value; 

     mailProvider.SendMail(fromAddress, toAddress, mailSubject, mailBody.ToString()); 
    } 

回答

1

HttpContext.Current.Request.ApplicationPath。

1

您可以使用HttpContext.Current類來從Page類的外部引用當前頁面實例。

在你的情況,你要使用:

HttpContext.Current.Request 

相反的:

base.Request 
+0

這是一個很酷的gravatar!感謝您的解決方案,約翰剛剛在你的帖子:-) – IrishChieftain 2009-05-29 13:38:19

1

你引用「基地」其引用的頁面或UI代碼中生活時,UserControl對象。現在你已經將它從代碼中移出來了,新的類不會從這兩個類中派生出來,所以你必須改用HttpContext.Current.Request