title: Wrapping all responses
body: <p>I want to wrap all my http responses.<br>
  For example we have an action which returns some JSON data:</p>
  
  <pre><code>public IActionResult Get() 
  {
      var res = new
          {
              MessageBody = "Test",
              SomeData = 1
          };
  
          return Ok(res);
  }
  </code></pre>
  
  <p>I want my response looks like:</p>
  
  <pre><code>{    
      "StatusCode":200,
      "Result":
      {
          "MessageBody ":"Test",
          "SomeData":1        
      }
  }
  </code></pre>
  
  <p>If there is error then response must contain <code>ErrorMessage</code> field in a response.</p>
  
  <p>In the mvc 5 I used the <code>DelegationHandler</code>, but in the asp.net core this class is not implemented. Now, we have to use middlewares.</p>
  
  <p>This is code for mvc 5:</p>
  
  <pre><code>public class WrappingHandler : DelegatingHandler
  {
      protected override async Task&lt;HttpResponseMessage&gt; SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
      {
          var response = await base.SendAsync(request, cancellationToken);
  
          return BuildApiResponse(request, response);
      }
  
      private static HttpResponseMessage BuildApiResponse(HttpRequestMessage request, HttpResponseMessage response)
      {
          object content;
          string errorMessage = null;
  
          if (response.TryGetContentValue(out content) &amp;&amp; !response.IsSuccessStatusCode)
          {
              HttpError error = content as HttpError;
  
              if (error != null)
              {
                  content = null;
                  errorMessage = error.Message;
  
  #if DEBUG
                  errorMessage = string.Concat(errorMessage, error.ExceptionMessage, error.StackTrace);
  #endif
              }
          }
  
          var newResponse = request.CreateResponse(response.StatusCode, new ApiResponse(response.StatusCode, content, errorMessage));
  
          foreach (var header in response.Headers)
          {
              newResponse.Headers.Add(header.Key, header.Value);
          }
  
          return newResponse;
      }
  }
  </code></pre>
  
  <p>and, a middleware for asp.net core. There are no <code>TryGetContentValue</code>, <code>HttpError</code> and other stuff in asp.net core. So, I am trying to read response body first:</p>
  
  <pre><code> public class FormatApiResponseMiddleware
   {
          private readonly RequestDelegate _next;
  
          public FormatApiResponseMiddleware(RequestDelegate next)
          {
              _next = next;
          }
  
          private bool IsSuccessStatusCode(int statusCode)
          {
              return (statusCode &gt;= 200) &amp;&amp; (statusCode &lt;= 299);
  
          }
  
          public async Task Invoke(HttpContext context)
          {
              object content = null;
              string errorMessage = null;
  
              if (!IsSuccessStatusCode(context.Response.StatusCode))
              {
                  content = null;
                  //how to get error 
              }
  
              var body= context.Response.Body;
          }
  }
  </code></pre>
  
  <p>But, <code>Body</code> stream has <code>CanRead</code> equal <code>false</code> and I get error that stream cannot be read. How to properly wrap response?</p>