惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Vercel News
Vercel News
The GitHub Blog
The GitHub Blog
博客园 - 【当耐特】
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Recent Announcements
Recent Announcements
D
Docker
GbyAI
GbyAI
酷 壳 – CoolShell
酷 壳 – CoolShell
WordPress大学
WordPress大学
The Cloudflare Blog
雷峰网
雷峰网
A
About on SuperTechFans
小众软件
小众软件
博客园 - Franky
博客园 - 聂微东
F
Full Disclosure
大猫的无限游戏
大猫的无限游戏
C
Check Point Blog
MongoDB | Blog
MongoDB | Blog
G
Google Developers Blog
Microsoft Azure Blog
Microsoft Azure Blog
U
Unit 42
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
V
V2EX
Engineering at Meta
Engineering at Meta
宝玉的分享
宝玉的分享
aimingoo的专栏
aimingoo的专栏
量子位
P
Proofpoint News Feed
Hugging Face - Blog
Hugging Face - Blog
博客园_首页
罗磊的独立博客
Martin Fowler
Martin Fowler
D
DataBreaches.Net
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
S
Secure Thoughts
Project Zero
Project Zero
L
LangChain Blog
阮一峰的网络日志
阮一峰的网络日志
C
Cybersecurity and Infrastructure Security Agency CISA
T
Tailwind CSS Blog
S
Schneier on Security
Blog — PlanetScale
Blog — PlanetScale
The Hacker News
The Hacker News
Spread Privacy
Spread Privacy
Security Latest
Security Latest
NISL@THU
NISL@THU
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
C
CXSECURITY Database RSS Feed - CXSecurity.com
J
Java Code Geeks

博客园 - 青玄鸟

.NET 中优雅处理 Server-Sent Events 请求取消 vue3.0 + ts 实现上传工厂(oss与cos) Dapr 订阅者参数无法正确反序列化问题 .NET 代码整洁手册 Blazor项目通过docker和nginx部署为静态站点的步骤 Moq mock 方法返回null空指针异常 基于接口隔离原则的依赖注入实现 HttpClient with Stream HttpClient 基本使用 值对象的封装 只读集合类型属性实现 适配器模式 模板模式 最少知识原则 单例模式 抽象工厂 简单工厂、工厂方法、抽象工厂 工厂方法模式 使用 Visual Studio Code创建和执行T-SQL
HttpClient partial update
青玄鸟 · 2020-04-28 · via 博客园 - 青玄鸟

full update 与 partial update

partial update:发送需要对远程资源做的变更(集合)

full update:发送变更后的资源实体

Json Patch

Json Patch 是描述一个json文档的变化的一种格式。可以避免在文档的一部分发生变化时发送整个文档。Json Patch在结合Http Patch Method 使用时,允许部分更新Api资源。Json Patch文档用Json格式表示:

原始文档:

{
  "baz": "qux",
  "foo": "bar"
}

Json Path:

[
  { "op": "replace", "path": "/baz", "value": "boo" },
  { "op": "add", "path": "/hello", "value": ["world"] },
  { "op": "remove", "path": "/foo" }
]

更新后文档:

{
  "baz": "boo",
  "hello": ["world"]
}

Json Patch详情

实现

客户端

private async Task PatchResource(HttpClient httpClient)
        {
            var patchDoc = new JsonPatchDocument<DemoForUpdate>();
            patchDoc.Replace(d=>d.Title,"another title");
            patchDoc.Remove(d=>d.Description);

            var serializedChangeSet = JsonConvert.SerializeObject(patchDoc);

            var request = new HttpRequestMessage(HttpMethod.Patch, "api/demos/163");
            request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            request.Content = new StringContent(serializedChangeSet);
            request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

            var response = await httpClient.SendAsync(request);
            response.EnsureSuccessStatusCode();

            var content =await response.Content.ReadAsStringAsync();
            var updateDemo = JsonConvert.DeserializeObject<Demo>(content);
        }

服务端

        [HttpPatch("{demoId}")]
        public async Task<IActionResult> PartiallyUpdateMovie(int demoId, 
            [FromBody] JsonPatchDocument<Models.DemoForUpdate> patchDoc)
        {
            var demoEntity = await _demosRepository.GetDemoAsync(demoId);
            if (demoEntity == null)
            {
                return NotFound();
            }

            var demoToPatch = Mapper.Map<Models.DemoForUpdate>(demoEntity );

            patchDoc.ApplyTo(demoToPatch, ModelState);
              
            if (!ModelState.IsValid)
            {
                return new UnprocessableEntityObjectResult(ModelState);
            }
            Mapper.Map(demoToPatch, demoEntity );

            _demosRepository.UpdateMovie(demoEntity);

            await _demosRepository.SaveChangesAsync();

            return Ok(_mapper.Map<Models.Demo>(demoEntity));
        }