/
aprogrammer
/
dotnet-docs
Обзор
Документация
Войти
/
aprogrammer
/
dotnet-docs
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
docs/core/extensions/snippets/http/basic/ItemService.cs
50 строк
1 KB
David Pine
Upgrade TFMs to .NET 8.0, and packages (#38752)
15 дек 2023, 16:49
Не верифицирован
15 дек 2023, 16:49
114b8f9
Код
Авторство
О чём код?
using System.Net.Mime; using System.Text; using System.Text.Json; using Shared; namespace BasicHttp.Example; public sealed class ItemService(HttpClient httpClient) : IDisposable { // <Create> public async Task CreateItemAsync(Item item) { using StringContent json = new( JsonSerializer.Serialize(item, new JsonSerializerOptions(JsonSerializerDefaults.Web)), Encoding.UTF8, MediaTypeNames.Application.Json); using HttpResponseMessage httpResponse = await httpClient.PostAsync("/api/items", json); httpResponse.EnsureSuccessStatusCode(); } // </Create> // <Update> public async Task UpdateItemAsync(Item item) { using StringContent json = new( JsonSerializer.Serialize(item, new JsonSerializerOptions(JsonSerializerDefaults.Web)), Encoding.UTF8, MediaTypeNames.Application.Json); using HttpResponseMessage httpResponse = await httpClient.PutAsync($"/api/items/{item.Id}", json); httpResponse.EnsureSuccessStatusCode(); } // </Update> // <Delete> public async Task DeleteItemAsync(Guid id) { using HttpResponseMessage httpResponse = await httpClient.DeleteAsync($"/api/items/{id}"); httpResponse.EnsureSuccessStatusCode(); } // </Delete> void IDisposable.Dispose() => httpClient?.Dispose(); }