显示一个404未找到页面的ASP。网络核心MVC 6

时间:2021-07-05 04:03:53

I am using the middle-ware below to set up error pages for HTTP status codes 400 to 599. So visiting /error/400 shows a 400 Bad Request error page.

我正在使用下面的中间软件为HTTP状态码400到599设置错误页面。所以访问/错误/400显示了一个400错误请求错误页面。

application.UseStatusCodePagesWithReExecute("/error/{0}");

[Route("[controller]")]
public class ErrorController : Controller
{
    [HttpGet("{statusCode}")]
    public IActionResult Error(int statusCode)
    {
        this.Response.StatusCode = statusCode;
        return this.View(statusCode);
    }
}

However, visiting /this-page-does-not-exist results in a generic IIS 404 Not Found error page.

但是,访问/this-page- doesn ' t-exist将导致一个通用的IIS 404未找到错误页面。

Is there a way to handle requests that do not match any routes? How can I handle this type of request before IIS takes over? Ideally I would like to forward the request to /error/404 so that my error controller can handle it.

有办法处理不匹配任何路由的请求吗?在IIS接管之前,我如何处理这种类型的请求?理想情况下,我希望将请求转发到/error/404,以便我的错误控制器能够处理它。

In ASP.NET 4.6 MVC 5, we had to use the httpErrors section in the Web.config file to do this.

在ASP。NET 4.6 MVC 5,我们必须使用Web中的httpErrors部分。配置文件。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <httpErrors errorMode="Custom" existingResponse="Replace">
      <remove statusCode="404" />
      <error statusCode="404" responseMode="ExecuteURL" path="/error/404/" />
    </httpErrors>
  </system.webServer>
</configuration>

1 个解决方案

#1


2  

Based on this SO item, IIS gets the 404 (and therefore handles it) before it reaches UseStatusCodePagesWithReExecute.

基于此SO项,IIS在到达UseStatusCodePagesWithReExecute之前将获得404(并处理它)。

Have you tried this: https://github.com/aspnet/Diagnostics/issues/144? It suggests terminating the request that received a 404 so it does not go to IIS to handle. And here's the code to add to your Startup to do so:

您尝试过这个:https://github.com/aspnet/diagnostics s/issues/144?它建议终止接收404的请求,这样它就不会去IIS处理了。下面是要添加到你的初创公司的代码:

app.Run(context =>
{
   context.Response.StatusCode = 404;
   return Task.FromResult(0);
});

This appears to be an IIS-only issue.

这似乎是唯一的iis问题。

#1


2  

Based on this SO item, IIS gets the 404 (and therefore handles it) before it reaches UseStatusCodePagesWithReExecute.

基于此SO项,IIS在到达UseStatusCodePagesWithReExecute之前将获得404(并处理它)。

Have you tried this: https://github.com/aspnet/Diagnostics/issues/144? It suggests terminating the request that received a 404 so it does not go to IIS to handle. And here's the code to add to your Startup to do so:

您尝试过这个:https://github.com/aspnet/diagnostics s/issues/144?它建议终止接收404的请求,这样它就不会去IIS处理了。下面是要添加到你的初创公司的代码:

app.Run(context =>
{
   context.Response.StatusCode = 404;
   return Task.FromResult(0);
});

This appears to be an IIS-only issue.

这似乎是唯一的iis问题。