我有 EPiServer 应用程序,当加载某些配置或 EPiServer 初始化管道中发生异常时,有时可能会在应用程序启动时引发异常。我已将 customErrors 配置为重定向到/Error.htm 页面,并且我正在 global.asax 的 Application_EndRequest 事件中处理此页面的响应状态代码,以返回正确的状态代码,如下所示:
protected void Application_EndRequest(object sender, EventArgs e)
{
if (Request.Url.AbsolutePath.EndsWith("Error.htm"))
{
Response.StatusCode = 500;
Response.TrySkipIisCustomErrors = true;
}
}
当加载应用程序时发生异常,但在加载配置文件(在我的例子中为 episerver.config)或 EPiServer 初始化管道中发生异常(EPiServer 中存在一个错误)时,它效果很好。
尝试创建 IHttpModule,但未初始化。尝试在 Application_Error 中添加错误处理,但它也没有被触发。
似乎 ASP.NET 正在处理这些异常,因为它正确重定向到我的 Error.htm 页面,但它设置状态代码 304。而且我找不到进入管道来更改状态代码的方法。
我需要错误页面的 500 状态代码来配置负载均衡器以取消配置错误的服务器。
更新
我已将自定义错误设置为关闭,以便将正确的状态代码发送到 IIS:
<customErrors mode="Off" defaultRedirect="/Error.htm"></customErrors>
已配置 httpError 部分以重定向到 Error.htm,但完整的异常详细信息会在本地和远程显示。当我添加existingResponse =“Replace”时,由于重定向循环,它会引发异常:
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="500" subStatusCode="-1" />
<error statusCode="500" prefixLanguageFilePath="" path="Error.htm" responseMode="Redirect" />
</httpErrors>
已将 httpError 部分配置为 ExecuteURL,但仍会在本地和远程显示完整的异常详细信息。当我添加existingResponse =“Replace”时,它仍然在本地和远程显示完整的异常详细信息:
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="500" subStatusCode="-1" />
<error statusCode="500" prefixLanguageFilePath="" path="/Error.htm" responseMode="ExecuteURL" />
</httpErrors>
如果我设置responseMode =“File”和existingResponse =“Replace”,它会在本地和远程显示Error.htm:
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="500" subStatusCode="-1" />
<error statusCode="500" prefixLanguageFilePath="" path="Error.htm" responseMode="File" />
</httpErrors>
仍然无法在本地获取错误详细信息并远程获取 Error.htm。
最佳答案
在 EPiServer 7 及更高版本中,您可以使用 globalErrorHandling
来设置预期行为。
要通过 httpErrors/customErrors 完成错误处理,您可以将参数设置为 Off
:
<episerver>
<applicationSettings globalErrorHandling="Off" />
</episerver>
可能的值为“RemoteOnly|On|Off”
,如下所述:http://world.episerver.com/documentation/Items/Developers-Guide/Episerver-CMS/8/Configuration/Configuring-episerver/
您也可以在后台访问此参数(这将在保存时编辑您的 web.config):
关于asp.net - 在 ASP.NET 中加载配置文件发生异常时,如何为错误页面响应设置状态代码 500,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16187156/