diff options
author | crupest <crupest@outlook.com> | 2022-12-04 18:11:06 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2022-12-20 20:32:52 +0800 |
commit | 476a6141b71d3def4a777ce934fec0d0bca8cd28 (patch) | |
tree | 12eda3d48fefb733b11e675724481d75dc65d319 /docker/crupest-api/CrupestApi/CrupestApi.Commons/Json.cs | |
parent | cdfb5781c8028a08051e0dbe6b7a2e39013e0f72 (diff) | |
download | crupest-476a6141b71d3def4a777ce934fec0d0bca8cd28.tar.gz crupest-476a6141b71d3def4a777ce934fec0d0bca8cd28.tar.bz2 crupest-476a6141b71d3def4a777ce934fec0d0bca8cd28.zip |
Develop secret api. v4
Diffstat (limited to 'docker/crupest-api/CrupestApi/CrupestApi.Commons/Json.cs')
-rw-r--r-- | docker/crupest-api/CrupestApi/CrupestApi.Commons/Json.cs | 15 |
1 files changed, 14 insertions, 1 deletions
diff --git a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Json.cs b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Json.cs index 8409a96..1d8106c 100644 --- a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Json.cs +++ b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Json.cs @@ -18,8 +18,16 @@ public static class CrupestApiJsonExtensions return services; } + public static async Task WriteJsonAsync<T>(this HttpResponse response, T bodyObject, int statusCode, HttpResponseAction? beforeWriteBody, CancellationToken cancellationToken = default) + { + await response.WriteJsonAsync(bodyObject, statusCode, (context) => + { + beforeWriteBody?.Invoke(context); + return Task.CompletedTask; + }, cancellationToken); + } - public static async Task WriteJsonAsync<T>(this HttpResponse response, T bodyObject, int statusCode = 200, HttpResponseAction? beforeWriteBody = null, CancellationToken cancellationToken = default) + public static async Task WriteJsonAsync<T>(this HttpResponse response, T bodyObject, int statusCode = 200, AsyncHttpResponseAction? beforeWriteBody = null, CancellationToken cancellationToken = default) { var jsonOptions = response.HttpContext.RequestServices.GetRequiredService<IOptionsSnapshot<JsonSerializerOptions>>(); byte[] json = JsonSerializer.SerializeToUtf8Bytes<T>(bodyObject, jsonOptions.Value); @@ -36,4 +44,9 @@ public static class CrupestApiJsonExtensions await response.Body.WriteAsync(json, cancellationToken); } + + public static async Task WriteMessageAsync(this HttpResponse response, string message, int statusCode = 200, HttpResponseAction? beforeWriteBody = null, CancellationToken cancellationToken = default) + { + await response.WriteJsonAsync(new ErrorBody(message), statusCode: statusCode, beforeWriteBody, cancellationToken); + } } |