asp.net - asp.net core web api 中是否有类似于 DelegatingHandler 的类?

标签 asp.net asp.net-core asp.net-core-webapi

我正在尝试构建 asp.net core WEB API 服务,其中我想发送一致的响应,并为所有请求返回类似的结构。

在早期版本的WEB API中,我们有DelegatingHandler,使用它我们可以拦截响应并将元数据信息添加到响应中。

以下 URL 的一些解释:https://www.devtrends.co.uk/blog/wrapping-asp.net-web-api-responses-for-consistency-and-to-provide-additional-information

我尝试了 OWIN Middleware ,但没​​有得到结果。

有没有办法在核心WEB API中实现它?

提前致谢。

最佳答案

不知道您到底尝试过什么,但在 ASP.NET Core 中,中间件正是您所需要的,如下所示:

Middleware is software that is assembled into an application pipeline to handle requests and responses. Each component chooses whether to pass the request on to the next component in the pipeline, and can perform certain actions before and after the next component is invoked in the pipeline.

Migrating HTTP Modules to Middleware文档中的部分解释了 ASP.NET Core 和以前版本中的请求管道之间的区别,并且应该帮助您了解如何编写以您需要的方式运行的中间件。

还要查看相关的SO问题:Registering a new DelegatingHandler in ASP.NET Core Web API

关于asp.net - asp.net core web api 中是否有类似于 DelegatingHandler 的类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44464461/

相关文章:

entity-framework - Scaffold-DbContext 为没有主键的表创建模型

asp.net-mvc - 如何使用 Postman 使用 cookie 身份验证测试 ASP.NET Core Web API?

c# - ASP.NET 动态数据编辑复杂对象

asp.net - 显示验证消息时表格行向下移动,未对齐

asp.net - web.config 中的多重/不同身份验证设置

javascript - (ASP.NET Core)如何更新 View 上的 View 组件

c# - .Net Core 依赖注入(inject)从构造函数中注入(inject)

c# - 模拟一个方法返回 true 仍然返回 false

具有 Net Core Web API 的 Azure 事件中心

c# - 单击按钮时显示模态弹出窗口