The way the ASP. NET configuration file is configured is actually the most clearly written in MSDN. It's a pity that I've never known the power of MSDN before.
First address: http://msdn.microsoft.com/zh-cn/library/dayb112d (v=vs.80). aspx, and then analyze it individually. I hope I can finish this series of things from beginning to end. In order not to let myself give up too early, I decided to learn from the configuration files I used, and then gradually contact those unfamiliar, and less use.
First, customErrors elements
Properties |
Description |
Defaultredirect |
Specifies the default URL to which the browser is directed when an error occurs. If this property is not specified, a generic error is displayed. An optional property. The URL can be absolute (such as www.contoso.com/ErrorPage.htm) or relative. A relative URL, such as /errorpage.htm, is relative to the Web. config file that specifies a URL for the property, rather than to the webpage where the error occurred. A URL that begins with a character (~), such as ~/errorpage.htm, indicates that the specified URL is relative to the root path of the application. |
Mode |
Specifies whether custom errors are enabled or disabled, or only custom errors are displayed to remote clients. The Required property. Optional values and descriptions: On specifies that custom errors are enabled. If defaultredirect is not specified, the user will see a generic error. OFF specifies that custom errors are disabled. This allows a detailed error of the standard to be displayed. REMOTEONLY Specifies that custom errors are displayed only to remote clients and that an ASP. NET error is displayed to the local host. This is the default value. The default value is RemoteOnly. |
2. Location
Customererrors, configuration-> system.web//This element is placed under <system.web> under <configuration> node
3. Child elements
Elements |
Description |
Error |
Specifies a custom error page for the given HTTP status code. An optional element. The error token can occur more than once. Each occurrence of a child tag defines a custom error condition. |
4. Configuration Example:
<configuration> <system.web> <customerrors defaultredirect= "defaulterror.htm"//error occurred, Redirect to defaulterror.htm mode= "RemoteOnly" >//Display detailed error messages only for Local users <error statuscode= "500"/ /for 500 error, jump to 500error.htm redirect= "500error.htm"/> </customErrors> </system.web> </configuration>
5. configuration Section Handler
This configuration node corresponds to the configuration section class in asp:
System.Web.Configuration.CustomErrorsSection
Take a look at the main public properties in the class:
Property |
Description |
Defaultredirect |
Gets or sets the default URL for redirection. |
Elementinformation |
Gets the Elementinformation object that contains the ConfigurationElement object's non-customizable information and functionality. (inherited from ConfigurationElement.) ) |
Errors |
Gets a collection of Customerror objects, which is the following <error> configuration section. |
Lockallattributesexcept |
Gets the collection of properties that are locked. (inherited from ConfigurationElement.) ) |
Lockallelementsexcept |
Gets the collection of elements that are locked. (inherited from ConfigurationElement.) ) |
Lockattributes |
Gets the collection of properties that are locked. (inherited from ConfigurationElement.) ) |
Lockelements |
Gets the collection of elements that are locked. (inherited from ConfigurationElement.) ) |
Lockitem |
Gets or sets a value that indicates whether the element is locked. (inherited from ConfigurationElement.) ) |
Mode |
Gets or sets the error display mode. |
Sectioninformation |
Gets a Sectioninformation object that contains non-customizable information and functionality for the ConfigurationSection object. (inherited from ConfigurationSection.) ) |
Here's how to read and set the configuration section in a program like this:
Public ActionResult Index () {//<customerrors defaultredirect= "defaulterror.htm" mode= "Off" &G T <error statuscode= "$" redirect= "500.htm"/>//</customerrors>//customerrorssection Customerrorssection = Configurationmanager.getsection ("System.web/customerrors") as customerrorssection; This can also be obtained, but note the way inside the path is customerrorssection CES = (customerrorssection) Webconfigurationmanager.openwebconfigura tion ("/"). GetSection ("System.web/customerrors"); Response.Write (CES. defaultredirect); Output defaulterror.htm Customerrorsmode mode = CES. Mode; Response.Write (mode); Output Off customerror CE = CES. Errors[0]; Gets the first child <error> node under its Response.Write (CE. StatusCode); Output of Response.Write (CE. Redirect); Output 500.htm elementinformation eleinfo = CES. Elementinformation; Element Information Response.Write (Eleinfo.linenumber); The output 14 is exactly the line number of the customerrors Web. config System.Configuration.Configuration c = CES. Currentconfiguration; Reference Response.Write (CES) of the current configuration object. IsReadOnly ()); The output False indicates whether the node is read-only Response.Write (CES). Lockitem); Output False whether the element Response.Write (CES) has been locked. Redirectmode); Output Responseredirect If an enumeration redirects the user to a custom error page, the requested URL should be changed sectioninformation SI = CES. Sectioninformation; Response.Write (SI. Name); Output customErrors return View (); }