標籤:blog http strong 檔案 os cti
Asp.net設定檔的配置方式,其實在MSDN裡面是寫得最清楚的了。可惜之前一直未曾瞭解到MSDN的強大。
先貼個地址:http://msdn.microsoft.com/zh-cn/library/dayb112d(v=vs.80).aspx,然後逐個分析。我希望自己能夠從頭到尾認真學完這系列東西。為了不至於讓自己太早放棄,我決定從自己用過的設定檔學起,然後逐漸接觸那些比較生疏,和少用的。
一、customErrors 元素
| 屬性 |
說明 |
|
defaultRedirect |
指定出錯時將瀏覽器定向到的預設 URL。如果未指定該屬性,則顯示一般性錯誤。
可選的屬性。
URL 可以是絕對的(如 www.contoso.com/ErrorPage.htm)或相對的。相對 URL(如 /ErrorPage.htm)是相對於為該屬性指定 URL 的 Web.config 檔案,而不是相對於發生錯誤的網頁。以字元 (~) 開頭的 URL(如 ~/ErrorPage.htm)表示指定的 URL 是相對於應用程式的根路徑。 |
|
mode |
指定是啟用或禁用自訂錯誤,還是僅向遠程用戶端顯示自訂錯誤。
必選的屬性。
可選值以及說明:
On 指定啟用自訂錯誤。如果未指定 defaultRedirect,使用者將看到一般性錯誤。
Off 指定禁用自訂錯誤。這允許顯示標準的詳細錯誤。
RemoteOnly 指定僅向遠程用戶端顯示自訂錯誤並且向本地主機顯示 ASP.NET 錯誤。這是預設值。
預設值為 RemoteOnly。 |
2、位置
customerErrors -> configuration-> system.web //該元素放於<system.web>下的<configuration>節點下
3、子項目
| 元素 |
說明 |
|
error |
指定給定 HTTP 狀態碼的自訂錯誤頁。
可選的元素。
錯誤標記可以出現多次。子標記的每一次出現均定義一個自訂錯誤條件。 |
4、配置樣本:
<configuration> <system.web> <customErrors defaultRedirect="defaultError.htm" //發生錯誤時,重新導向到defaultError.htm mode="RemoteOnly"> //僅僅對本機使用者顯示詳細錯誤資訊 <error statusCode="500" //針對500錯誤,跳轉到500Error.htm redirect="500Error.htm"/> </customErrors> </system.web></configuration>
5、配置節處理常式
該配置節點對應Asp.net中的配置節類為:
System.Web.Configuration.CustomErrorsSection
來看看在類的主要公用屬性:
| 屬性 |
說明 |
| DefaultRedirect |
擷取或設定重新導向的預設 URL。 |
| ElementInformation |
擷取包含 ConfigurationElement 對象的不可自訂的資訊和功能的 ElementInformation 對象。 (從 ConfigurationElement 繼承。) |
| Errors |
擷取 CustomError 對象的集合,也就是其下面的<error>配置節。 |
| LockAllAttributesExcept |
擷取被鎖定的屬性的集合。 (從 ConfigurationElement 繼承。) |
| LockAllElementsExcept |
擷取被鎖定的元素的集合。 (從 ConfigurationElement 繼承。) |
| LockAttributes |
擷取被鎖定的屬性的集合。 (從 ConfigurationElement 繼承。) |
| LockElements |
擷取被鎖定的元素的集合。 (從 ConfigurationElement 繼承。) |
| LockItem |
擷取或設定一個值,該值指示是否鎖定該元素。 (從 ConfigurationElement 繼承。) |
| Mode |
擷取或設定錯誤顯示模式。 |
| SectionInformation |
擷取一個 SectionInformation 對象,該對象包含 ConfigurationSection 對象的不可自訂的資訊和功能。 (從 ConfigurationSection 繼承。) |
下面以程式樣本如何在程式中讀取和設定配置節:
public ActionResult Index() { //<customErrors defaultRedirect="defaultError.htm" mode="Off"> // <error statusCode="500" redirect="500.htm"/> //</customErrors> //CustomErrorsSection customErrorsSection = ConfigurationManager.GetSection("system.web/customErrors") as CustomErrorsSection; //這樣擷取也可以,但是要注意裡面的路徑的寫法 CustomErrorsSection CES = (CustomErrorsSection)WebConfigurationManager.OpenWebConfiguration("/").GetSection("system.web/customErrors"); Response.Write(CES.DefaultRedirect); //輸出 defaultError.htm CustomErrorsMode mode = CES.Mode; Response.Write(mode); //輸出 Off CustomError CE = CES.Errors[0]; //擷取其下的第一個子<error>節點 Response.Write(CE.StatusCode); //輸出 500 Response.Write(CE.Redirect); //輸出 500.htm ElementInformation EleInfo = CES.ElementInformation; //元素資訊 Response.Write(EleInfo.LineNumber); //輸出 14 恰好是customErrors所在Web.Config的行號 System.Configuration.Configuration c = CES.CurrentConfiguration; //當前Configuration對象的引用 Response.Write(CES.IsReadOnly()); //輸出 False 指示該節點是否為唯讀 Response.Write(CES.LockItem); //輸出 False 是否鎖定該元素 Response.Write(CES.RedirectMode); //輸出 ResponseRedirect 一個枚舉 將使用者重新導向到自訂錯誤頁面時,是否應該變更要求的URL SectionInformation SI = CES.SectionInformation; Response.Write(SI.Name); //輸出 customErrors return View(); }