csharp C#Post,Put和Patch作为JSON异步扩展

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp C#Post,Put和Patch作为JSON异步扩展相关的知识,希望对你有一定的参考价值。

using System;
using System.Net.Http;
using System.Net.Http.Formatting;
using System.Threading.Tasks;

namespace MyProject.Extensions
{
    public static class HttpClientEx
    {
        public const string MimeJson = "application/json";

        public static Task<HttpResponseMessage> PatchAsync(this HttpClient client, string requestUri, HttpContent content)
        {
            HttpRequestMessage request = new HttpRequestMessage
            {
                Method = new HttpMethod("PATCH"),
                RequestUri = new Uri(client.BaseAddress + requestUri),
                Content = content,
            };

            return client.SendAsync(request);
        }

        public static Task<HttpResponseMessage> PostJsonAsync(this HttpClient client, string requestUri, Type type, object value)
        {
            return client.PostAsync(requestUri, new ObjectContent(type, value, new JsonMediaTypeFormatter(), MimeJson));
        }

        public static Task<HttpResponseMessage> PutJsonAsync(this HttpClient client, string requestUri, Type type, object value)
        {
            return client.PutAsync(requestUri, new ObjectContent(type, value, new JsonMediaTypeFormatter(), MimeJson));
        }

        public static Task<HttpResponseMessage> PatchJsonAsync(this HttpClient client, string requestUri, Type type, object value)
        {
            return client.PatchAsync(requestUri, new ObjectContent(type, value, new JsonMediaTypeFormatter(), MimeJson));
        }
    }
}

以上是关于csharp C#Post,Put和Patch作为JSON异步扩展的主要内容,如果未能解决你的问题,请参考以下文章

post、get、put、patch、Delete的区别

重定向 PATCH 和 PUT 路由轨道 5 以使用 POST

[Rails]表单提交时,post与patch的内部转化

[Rails]表单提交时,post与patch的内部转化

HTTP POST, PUT PATCH

RESTFul 中PUT POST PATCH的区别