zoukankan      html  css  js  c++  java
  • Asp.Net customErrors与httpErrors的区别

    customErrors

    • Asp.Net级别的错误处理程序,只处理Asp.Net应用抛出的异常(404,403,500。。)
    • 在IIS7+的服务器依然可用(IIS7之前就引进了)
    • 静态文件(如.jpg.htm.js等)不会被处理

    httpErrors

    • IIS级别的错误信息处理程序,IIS根据请求指定错误页面
    • 自IIS7引进
    • 处理包括ASP.NET应用及ASP.NET之外的应用(ASP.NET能管的 它会管,ASP.NET不能管得它也管)
    • 所有的文件和URL都处理

    从对比中能看出 在IIS7之后  就没必要再用customErrors了,一切httpErrors都可以办了。

    <httpErrors errorMode="Custom" existingResponse="Replace">
        <remove statusCode="403" subStatusCode="-1" />
        <remove statusCode="404" subStatusCode="-1" />
        <error statusCode="403" prefixLanguageFilePath="" path="/403.png" responseMode="ExecuteURL" />
        <error statusCode="404" path="/404.aspx" responseMode="ExecuteURL" /> 
    </httpErrors>

    其实还可以用一个clear标签代替多个remove。如下

    <httpErrors errorMode="Custom" existingResponse="Replace">
        <clear />
        <error statusCode="403" prefixLanguageFilePath="" path="/403.png" responseMode="ExecuteURL" />
        <error statusCode="404" path="/404.aspx" responseMode="ExecuteURL" /> 
    </httpErrors>

    Note:ExecuteURL 只能用于同一个应用下的ASP.NET文件,如果想要重定向到另一个应用,或者一个完全不一样的完整的URL,我们需要将responseMode设为Redirect。

    <httpErrors errorMode="Custom" existingResponse="Replace">
      <clear />
      <error statusCode="404" path="http://www.bing.com" responseMode="Redirect"/>
    </httpErrors>

    现在通过不同的URL来看两者的区别

    给Web应用定义如下配置

    <system.web>
        <customErrors mode="On" defaultRedirect="Error.html">
            <error statusCode="403" redirect="/Error403" />
            <error statusCode="404" redirect="/Error404" />
            <error statusCode="500" redirect="/Error500" />
        </customErrors>
    </system.web>
    <system.webServer>
      <httpErrors errorMode="Custom" existingResponse="Auto" defaultResponseMode="ExecuteURL" >
        <remove statusCode="403"/>
        <remove statusCode="404"/>
        <remove statusCode="500"/>
        <error statusCode="403" responseMode="ExecuteURL" path="/Error403" />
        <error statusCode="404" responseMode="ExecuteURL" path="/Error404" />
        <error statusCode="500" responseMode="ExecuteURL" path="/Error500" />
      </httpErrors>
    </system.webServer>

    现在如果尝试访问以下链接,将会产生对应的错误

     
    URL Error StatusCode
    /aaaaaa  httpErrors  404
    /aaaaaa.aspx customErrors  404
    /aaaaaa.jpg httpErrors  404
    /throw500.apx customErrors  500
    /throw500 customErrors  500

    1. 一般情况 customErrors标签上的model属性设为RemoteOnly,httpErrors上的errorModel设为DetailedLocalOnly
    2. 如果你将某个页面的StatusCode设为500,不要忘了设置如下属性
    context.Response.TrySkipIisCustomErrors = true;

    以下是微软官网说明:

    https://msdn.microsoft.com/zh-cn/library/system.web.httpresponse.tryskipiiscustomerrors.aspx

    TrySkipIisCustomErrors 属性仅在您的应用程序承载在 IIS 7.0 中使用。 在 IIS 7.0 中的经典模式下运行时 TrySkipIisCustomErrors 
    属性的默认值是 true。 在集成模式下,运行时 TrySkipIisCustomErrors 属性的默认值是 false

    补充

    errorMode有三个值,分别为Custom、DetailedLocalOnly、Detailed,意思为对用户与服务器端始终显示自定义页面、只能服务器端显示详细出错信息、对用户与服务器端始终显示详细出错信息。

    responseMode有File、ExecuteUrl、Redirect三个层,分别表示使用服务器端静态文件、可执行的URL、URL转向。

    注意:<httpErrors>与<customErrors>是不同的,前者主要用于处理http相关的错误信息,而后者主要是处理应用程序级的错误页转向。

    <!-- 自定义错误信息 
    设置 customErrors mode="On" 或 "RemoteOnly" 以启用自定义错误信息,或设置为 "Off" 以禁用自定义错误信息。 
    为每个要处理的错误添加 <error> 标记。 

    "On" 始终显示自定义(友好的)信息。 
    "Off" 始终显示详细的 ASP.NET 错误信息。 
    "RemoteOnly" 只对不在本地 Web 服务器上运行的 
    用户显示自定义(友好的)信息。出于安全目的,建议使用此设置,以便 
    不向远程客户端显示应用程序的详细信息。 
    --> 

  • 相关阅读:
    周末之个人杂想(十三)
    PowerTip of the DaySorting Multiple Properties
    PowerTip of the DayCreate Remoting Solutions
    PowerTip of the DayAdd Help to Your Functions
    PowerTip of the DayAcessing Function Parameters by Type
    PowerTip of the DayReplace Text in Files
    PowerTip of the DayAdding Extra Information
    PowerTip of the DayPrinting Results
    Win7下IIS 7.5配置SSAS(2008)远程访问
    PowerTip of the DayOpening Current Folder in Explorer
  • 原文地址:https://www.cnblogs.com/tianciliangen/p/8117917.html
Copyright © 2011-2022 走看看