我的网址如下:/ employees?employeeId = 3992,显示ID为3992的员工。工作正常,HTTP状态代码为200 OK。
但是当有人在URL中写入无效ID时,例如:/ employees?employeeId = 39920,我希望我的HTTP处理程序重写错误页面的路径,并发送HTTP状态代码404.
这是我正在使用的代码:
if (employee == null)
{
context.RewritePath("/error-page");
context.Response.StatusCode = 404; // if I remove this line, the error-page is displayed, and the StatusCode is 200.
}
当我将HTTP状态代码设置为404时,页面显示此标准ASP.NET 404错误页面。
答案 0 :(得分:0)
也请致电
context.Response.TrySkipIisCustomErrors = true;
例如:
if (employee == null)
{
context.Response.TrySkipIisCustomErrors = true;
context.RewritePath("/error-page");
context.Response.StatusCode = 404;
}