且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

ASP.net Core 2.0管道中的Intercept 401错误

更新时间:2023-02-16 10:50:28

请先使用此中间件:

app.Use(async (context, next) =>
{
    await next.Invoke();

    if(context.Response.StatusCode == StatusCodes.Status404NotFound)
    {
        await context.Response.WriteAsync("You seem to have mistyped the url");
    }              
});

未找到"将不会有任何异常.上下文响应的StatusCode的默认值为404.如果没有中间件修改响应,则404将返回给客户端.在这种情况下,我们设置了一个中间件,该中间件允许其他中间件首先通过等待next.Invoke().. Now来处理请求.现在,在返回该中间件时,它会检查StatusCode是否仍为404并写上您似乎输入了错误的网址"消息.

There won't be any exception for Not found. Default value of StatusCode for a context response is 404. If no middleware modifies the response , 404 will be returned to the client. In this case, we have set up a middleware that allows other middlewares to process the request first by awaiting next.Invoke()..Now, while returning back it checks if StatusCode is still 404 and writes a "you seem to have mistyped the url" message.