2 // System.Web.Configuration.CustomErrorsConfigHandler
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // (C) 2003 Novell, Inc. (http://www.novell.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Collections;
33 using System.Configuration;
37 namespace System.Web.Configuration
46 class CustomErrorsConfig
48 string defaultRedirect;
51 string configFilePath;
53 public CustomErrorsConfig (object parent, object context)
56 CustomErrorsConfig p = (CustomErrorsConfig) parent;
58 defaultRedirect = p.defaultRedirect;
59 if (p.redirects != null && p.redirects.Count > 0) {
60 redirects = new Hashtable ();
61 foreach (DictionaryEntry entry in p.redirects)
62 redirects [entry.Key] = entry.Value;
66 configFilePath = Path.GetDirectoryName ((string) context);
69 public string DefaultRedirect {
70 get { return defaultRedirect; }
71 set { defaultRedirect = value; }
74 public CustomErrorMode Mode {
79 public string ConfigFilePath {
80 get { return configFilePath; }
83 public string this [int statusCode] {
85 if (redirects == null)
88 return (string) redirects [statusCode];
92 if (redirects == null)
93 redirects = new Hashtable ();
95 // Overrides any previous setting for statusCode even in the same file
96 redirects [statusCode] = value;
101 class CustomErrorsConfigHandler : IConfigurationSectionHandler
103 public object Create (object parent, object context, XmlNode section)
105 CustomErrorsConfig config = new CustomErrorsConfig (parent, context);
107 string defaultRedirect = AttValue ("defaultRedirect", section);
108 if (defaultRedirect != null)
109 config.DefaultRedirect = defaultRedirect;
111 string mode = AttValue ("mode", section);
115 config.Mode = CustomErrorMode.On;
118 config.Mode = CustomErrorMode.Off;
121 config.Mode = CustomErrorMode.RemoteOnly;
124 ThrowException ("Invalid value for 'mode': " + mode, section);
129 if (section.Attributes != null && section.Attributes.Count != 0)
130 ThrowException ("Unrecognized attribute", section);
132 if (!section.HasChildNodes)
135 XmlNodeList children = section.ChildNodes;
136 foreach (XmlNode child in children) {
137 XmlNodeType ntype = child.NodeType;
138 if (ntype == XmlNodeType.Whitespace || ntype == XmlNodeType.Comment)
141 if (ntype != XmlNodeType.Element)
142 ThrowException ("Only elements allowed", child);
144 if (child.Name != "error")
145 ThrowException ("Unrecognized node: " + child.Name, child);
147 string statusCode = AttValue ("statusCode", child, false, false);
148 string redirect = AttValue ("redirect", child, false, false);
151 code = Int32.Parse (statusCode);
153 ThrowException ("Unable to parse 'statusCode': " + statusCode, child);
156 if (code < 100 || code >= 1000)
157 ThrowException ("Invalid value for 'statusCode': " + code, child);
159 config [code] = redirect;
165 // To save some typing...
166 static string AttValue (string name, XmlNode node, bool optional, bool allowEmpty)
168 return HandlersUtil.ExtractAttributeValue (name, node, optional, allowEmpty);
171 static string AttValue (string name, XmlNode node)
173 return HandlersUtil.ExtractAttributeValue (name, node, true);
176 static void ThrowException (string message, XmlNode node)
178 HandlersUtil.ThrowException (message, node);