asp.net-core - 如何禁用所有 API Controller 的状态代码页中间件?

标签 asp.net-core asp.net-core-2.1

状态代码页中间件适用于为所有 HTML 页面设置常见错误页面。但是,由于 Web API 会返回自己的错误响应,因此不应在 API Controller 中启用中间件。如何使用 ApiControllerAttribute 在所有 Controller 中禁用状态代码页中间件?

最佳答案

要运行 UseStatusCodePages 除了 API,您可以尝试像这样配置中间件:

app.MapWhen(
    httpcontext =>
    {
        return !httpcontext.Request.Path.Value.StartsWith("/api");
    },
    subApp =>
    {
        subApp.UseStatusCodePages();
    });

关于asp.net-core - 如何禁用所有 API Controller 的状态代码页中间件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53223817/

相关文章:

c# - .NET Core X509Certificate2 使用(Windows/IIS、Docker、Linux下)

c# - 如何仅更新 Entity Framework (核心)中的特定字段

c# - 定义并获取 ASP.Net Core (Session) 列表

c# - 如何编写使用 HttpContext 的服务器端单元测试?

c# - ASP.NET Core 2 中的问题配置选项

javascript - 无法在 Azure WebApp Linux (.Net core 2.1 MVC) 上运行 jsreportlocal

asp.net-core - 将 .NET Core 重新定位到 net471、net 472

asp.net - 结合CLR类库发布ASP.NET vNext/DNX

asp.net-core - 如何在 AspNetCore 2.2 NLog 中关闭 Microsoft 日志

asp.net-core-2.1 - 找不到 AddCommandLine .netcore 2.1