gpt4 book ai didi

除 404 错误外的 ASP.NET 自定义错误,

转载 作者:行者123 更新时间:2023-12-05 00:09:16 24 4
gpt4 key购买 nike

我设置了自定义错误如下..

protected void Application_Error(Object sender, EventArgs e)
{
Exception ex = HttpContext.Current.Server.GetLastError();
if (ex is HttpUnhandledException && ex.InnerException != null)
ex = ex.InnerException;
if (ex != null)
{
try
{
Logger.Log(LogEventType.UnhandledException, ex);
}
catch
{ }
}

HttpContext.Current.Server.ClearError();
Response.Redirect(MyCustomError);

}

我想知道是否可以检测这个错误是否是404错误,然后显示默认的404错误?

最佳答案

我不得不说我真的很困惑为什么你要清除发生的任何错误,然后首先重定向到自定义错误页面。

也许改变:

HttpContext.Current.Server.ClearError();
Response.Redirect(MyCustomError);
//to
if (!(ex is HttpException && 404 == ((HttpException)ex).getHttpCode())){
HttpContext.Current.Server.ClearError();
Response.Redirect(MyCustomError);
}

否则不要进行任何这些检查,并保留任何未找到的文件异常(exception)。让它们由您在 web.config 中定义的自定义错误处理程序处理。通过框架。
<customErrors mode="RemoteOnly" defaultRedirect="~/Error/500.htm">
<error statusCode="500" redirect="~/Error/500.htm"/>
<error statusCode="404" redirect="~/Error/404.htm"/>
</customErrors>

如果您没有看到处理,您希望将模式更改为“开”。

如果需要抛出 404,可能是因为请求项的 ID 已从数据库中删除,然后抛出相应的 HttpException 错误。

关于除 404 错误外的 ASP.NET 自定义错误,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/665005/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com