【发布时间】:2019-10-25 23:11:51
【问题描述】:
我正在尝试为 ASP.NET 4.5.2 Web 窗体项目设置 500 和 404 页面。当不存在的页面是 .ASPX 页面时,我让 404 工作。 - 我编写了代码来提供自定义 NotFound.aspx 页面作为响应。
当我使用 Web.config 为不存在的 HTML 页面提供 404.html 页面时,出现了我的问题。我的 404.html 页面位于根文件夹中。如果我使用根文件夹中不存在的 HTML 页面对此进行测试,则 404.html 会正确呈现。但是,如果我在不存在的子文件夹中使用不存在的 HTML 页面对其进行测试,则会提供 404.html,但 CSS/JS 路径不再正确?就像它正在将 CSS/JS 路径视为与我在测试中指定的不存在的子文件夹相关?
Global.asax.cs:
void Application_Error(object sender, EventArgs e)
{
Exception ex = Server.GetLastError();
if (ex != null && ex is HttpUnhandledException)
{
Server.ClearError();
Server.Transfer("~/Error.aspx", true);
}
if (ex != null)
{
var httpException = ex as HttpException;
if (httpException.GetHttpCode() == 404)
{
Server.ClearError();
Server.Transfer("~/NotFound.aspx", true);
}
}
}
Web.config:
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="404" />
<error statusCode="404" path="404.html" responseMode="File" />
</httpErrors>
【问题讨论】:
标签: asp.net webforms http-status-code-404