| | | 1 | | using ArturRios.Output; |
| | | 2 | | using Microsoft.AspNetCore.Http; |
| | | 3 | | |
| | | 4 | | namespace ArturRios.Util.WebApi.Extensions; |
| | | 5 | | |
| | | 6 | | /// <summary>Extension methods for writing output envelopes directly to an <see cref="HttpResponse"/>, outside MVC.</sum |
| | | 7 | | internal static class HttpResponseExtensions |
| | | 8 | | { |
| | | 9 | | /// <param name="response">The response to write to.</param> |
| | | 10 | | extension(HttpResponse response) |
| | | 11 | | { |
| | | 12 | | /// <summary>Sets the status code and writes <paramref name="output"/> as JSON, using the app's configured |
| | | 13 | | /// <c>Microsoft.AspNetCore.Http.Json.JsonOptions</c> (camelCase by default), so envelopes written by |
| | | 14 | | /// middlewares have the same shape as the ones MVC writes for controller results.</summary> |
| | | 15 | | /// <param name="statusCode">The HTTP status code.</param> |
| | | 16 | | /// <param name="output">The envelope to serialize.</param> |
| | | 17 | | public Task WriteOutputAsync<TOutput>(int statusCode, TOutput output) |
| | | 18 | | where TOutput : ProcessOutput |
| | | 19 | | { |
| | 15 | 20 | | response.StatusCode = statusCode; |
| | | 21 | | |
| | 15 | 22 | | return response.WriteAsJsonAsync(output); |
| | | 23 | | } |
| | | 24 | | } |
| | | 25 | | } |