更改 WCF WebApi HttpContent 响应
使用 WCF Web API,在应用程序逻辑运行之后但在返回给用户之前,我将如何更改响应的内容正文。目标是如果抑制状态内容为 true,我们:
- 将状态代码字段添加到内容正文
- 将响应上的状态代码更改为 200
我已经覆盖了 DelegatingChannel,并且在 SendAsnyc 中有一些如下所示的代码:
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
return base.SendAsync(request, cancellationToken).ContinueWith<HttpResponseMessage>(task =>
{
var response = task.Result;
if (CheckIfRequestHadSuppressStatusCode(request) == true)
{
string newResponse = (response.Content == null) ? "" : response.Content.ReadAsString();
newResponse = "<body>" +newResponse + "</body><statuscode>" + response.StatusCode + "</statuscode>";
response.StatusCode = HttpStatusCode.OK;
}
return response;
});
一个主要问题是这个 不处理 xml
和 Json
两者。我觉得一定有更好的方法来解决这个问题,因为这感觉很老套。
Using the WCF Web API how would I go about changing a response's content body after the application logic has been run but before it's returned to the user. The goal is if suppressstatuscontent is true we:
- Add a statuscode field to the content body
- Change the statuscode on the response to 200
I have overridden a DelegatingChannel and in the SendAsnyc have some code that looks like this:
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
return base.SendAsync(request, cancellationToken).ContinueWith<HttpResponseMessage>(task =>
{
var response = task.Result;
if (CheckIfRequestHadSuppressStatusCode(request) == true)
{
string newResponse = (response.Content == null) ? "" : response.Content.ReadAsString();
newResponse = "<body>" +newResponse + "</body><statuscode>" + response.StatusCode + "</statuscode>";
response.StatusCode = HttpStatusCode.OK;
}
return response;
});
A major problem is this doesn't handle BOTH, xml
and Json
. I feel like there must be a much better way to go about the problem as this feels very hacky.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定正确的方法,但我会尝试这样的方法:
您可以封装在 HttpContent 的专门版本(例如 XmlWithStatusContent 和 JsonWithStatusContent)中添加额外状态代码标记的代码。
I'm not sure of the right approach but I would try something like this:
You can encapsulate the code that adds the extra status code markup in specialized versions of HttpContent (e.g. XmlWithStatusContent and JsonWithStatusContent).
您可以将内容解析为 XML 或 JSON(您可以将该功能封装在它自己的类中),然后您就能够添加独立于格式的字段(独立于
SendAsync
不需要知道)。假设
Parse
会返回某种内容对象,您可以修改而无需知道格式是什么。这并不是一个真正好的解决方案,但它稍微封装了一些黑客行为。
更新:假设您可以从
HttpResponseMessage
派生并完全控制生成响应,那么您可以有专门的子类来处理它:在生成响应时,您创建 Xml/JsonHttpResponseMessage对象代替。然后你可以这样做:
You could parse the content as either XML or JSON (you can encapsulate that functionality in it's own class) which then gives you the ability to add the field independent of the format (independent in the sense that the
SendAsync
doesn't need to know).assuming
Parse
would return come kind of content object you can modify without having to know what the format is.It's not a really a nice solution but it encapsulates the hackiness away a bit.
Update: Assuming you can dereive from
HttpResponseMessage
and are in full control of generating the response then you could have specialised subclasses dealing with it:When generating the response you create Xml/JsonHttpResponseMessage objects instead. Then you can do: