您的位置:首页 > 编程语言 > ASP

Asp.net Web.Config - 配置元素customErrors

2013-06-05 14:20 405 查看
  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、[b]配置节处理程序[/b]

  该配置节点对应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();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: