使用OWIN中间件扩展WebApi响应 [英] Extending WebApi response using OWIN Middleware

查看:157
本文介绍了使用OWIN中间件扩展WebApi响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于OWIN的WebApi项目,我想用一些其他数据扩展WebApi调用的结果,例如,将本地化数据添加到响应中.

I have a WebApi project based on OWIN, and I wanted to extend the result of WebApi call with some additional data, for example add localization data to response.

最初的想法是将该逻辑注入到管道中,一旦我们获得API调用的结果,我就用包装器包装了json,该包装器将包含翻译和一些其他属性,例如执行时间.

Initial idea was to inject that logic in a pipeline and once we get a result of API call I just have wrap that json with wrapper that will contain translation and some additional properties like time of execution.

所以我写了自己的中间件,将其放在UseWebApi()之后,但未执行,因为WebApi在处理请求的情况下不会调用"Next"

So I wrote my own middleware put it after UseWebApi() but it's not executed because WebApi doesn't call "Next" in case it handles the request

所以问题是:

如何修改/扩展WebApi中间件返回的Json? 还有其他想法如何通过必须针对所有请求执行的操作来处理此类问题?

How can I modify/extend Json returned by WebApi middleware? Any other ideas how to handle such a problem with an action that has to be executed for all requests?

谢谢

推荐答案

关于中间件: 您需要将中间件放置在UseWebApi之前,并在调用下一个中间件之后放置您的逻辑;在这种情况下,Web API处理完请求后,您的代码可以很好地执行.

Regarding the middleware: you need to place your middleware before UseWebApi, and put your logic after invoking the next middleware; in this case your code well execute after Web API is done processing the request.

您可能还想查看以下博客文章,其中讨论了与您相同的情况: http://www.devtrends.co.uk/blog/wrapping-asp.net-web-api-responses-for-consistency-and-to-provide-additional-information

You might also want to check the following blog post, it discusses the same scenario of yours: http://www.devtrends.co.uk/blog/wrapping-asp.net-web-api-responses-for-consistency-and-to-provide-additional-information

这篇关于使用OWIN中间件扩展WebApi响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆