简体   繁体   English

更改 WCF WebApi HttpContent 响应

[英]Change WCF WebApi HttpContent response

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.使用 WCF Web API go 如何在应用程序逻辑运行后但返回给用户之前更改响应的内容主体。 The goal is if suppressstatuscontent is true we:目标是如果 suppressstatuscontent 为真,我们:

  • Add a statuscode field to the content body向内容主体添加状态代码字段
  • Change the statuscode on the response to 200将响应的状态码更改为 200

I have overridden a DelegatingChannel and in the SendAsnyc have some code that looks like this:我已经覆盖了一个 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;
   });

A major problem is this doesn't handle BOTH , xml and Json .一个主要问题是这不能同时处理xmlJson I feel like there must be a much better way to go about the problem as this feels very hacky.我觉得必须有更好的方法到 go 解决这个问题,因为这感觉很老套。

I'm not sure of the right approach but I would try something 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)
         {
            switch(response.Content.Headers.ContentType.MediaType)
            {
               case "application/xml":
                  response.Content = new XmlWithStatusContent(response.Content)
                  break;
               case "application/json":
                  response.Content = new JsonWithStatusContent(response.Content)
                  break;
            }

            response.StatusCode = HttpStatusCode.OK;                                 
         }
         
         return response;
      });
}

You can encapsulate the code that adds the extra status code markup in specialized versions of HttpContent (eg XmlWithStatusContent and JsonWithStatusContent).您可以将添加额外状态代码标记的代码封装在特定版本的 HttpContent(例如 XmlWithStatusContent 和 JsonWithStatusContent)中。

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).您可以将内容解析为 XML 或 JSON(您可以将该功能封装在它自己的类中),然后让您能够添加独立于格式的字段(在SendAsync不需要知道的意义上独立) .

var newResponse = ContentDecoder.Parse(response.Content).AddField("statuscode", response.StatusCode).ToString();

assuming Parse would return come kind of content object you can modify without having to know what the format is.假设Parse会返回类型为 object 的内容,您可以在不知道格式是什么的情况下进行修改。

It's not a really a nice solution but it encapsulates the hackiness away a bit.这不是一个很好的解决方案,但它封装了一些 hackiness。

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:更新:假设您可以从HttpResponseMessage派生并完全控制生成响应,那么您可以使用专门的子类来处理它:

interface IHttpResponseContent
{
    void AddField(string name, string value);
}

class XmlHttpResponseMessage : HttpResponseMessage, IHttpResponseContent
{
}

When generating the response you create Xml/JsonHttpResponseMessage objects instead.在生成响应时,您改为创建 Xml/JsonHttpResponseMessage 对象。 Then you can do:然后你可以这样做:

var newResponse = response as IHttpResponseContent;
if (newResponse != null)
{
    newResponse.AddField("statuscode", response.StatusCode);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM