我正在尝试构建 asp.net core WEB API 服务,其中我想发送一致的响应,并为所有请求返回类似的结构。
在早期版本的WEB API中,我们有DelegatingHandler,使用它我们可以拦截响应并将元数据信息添加到响应中。
我尝试了 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/