zoukankan      html  css  js  c++  java
  • 配置Web.config 元素CustomErrors

    一、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();
            }
    复制代码
  • 相关阅读:
    Linux 使用crontab定时备份Mysql数据库
    pdf.js 文字丢失问题 .cmaps
    indexOf IE8下的兼容
    Huplaodfiy---图片文件上传插件修改
    dateTimePicker日期时间插件-----限定节假日调休的可选择性
    【转】MyEclipse第一个Servlet程序
    input 的 placeholder属性在IE8下的兼容处理
    zepto的scrollTo,实现锚点跳转
    微信小程序
    mac 终端中添加tree命令显示文件目录结构
  • 原文地址:https://www.cnblogs.com/Javi/p/6542737.html
Copyright © 2011-2022 走看看