Skip to content

Commit 8793347

Browse files
kkbruceBruce Chen
andauthored
Add full JsonPatch example with ASP.NET Core 3.1 Web API project (#16824)
* Add full JsonPatch example with NetCore 3.1 mvc and webapi project * remove jsonpatch mvc example * remove GetJsonPatchInputFormatter Co-authored-by: Bruce Chen <brucechen@kingstono365.onmicrosoft.com>
1 parent 3a3a97f commit 8793347

10 files changed

Lines changed: 236 additions & 0 deletions

File tree

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
using JsonPatchSample.Models;
2+
using Microsoft.AspNetCore.JsonPatch;
3+
using Microsoft.AspNetCore.Mvc;
4+
using System.Collections.Generic;
5+
using System.Dynamic;
6+
7+
namespace JsonPatchSample.Controllers
8+
{
9+
[Route("jsonpatch/[action]")]
10+
[ApiController]
11+
public class HomeController : ControllerBase
12+
{
13+
#region snippet_PatchAction
14+
[HttpPatch]
15+
public IActionResult JsonPatchWithModelState(
16+
[FromBody] JsonPatchDocument<Customer> patchDoc)
17+
{
18+
if (patchDoc != null)
19+
{
20+
var customer = CreateCustomer();
21+
22+
patchDoc.ApplyTo(customer, ModelState);
23+
24+
if (!ModelState.IsValid)
25+
{
26+
return BadRequest(ModelState);
27+
}
28+
29+
return new ObjectResult(customer);
30+
}
31+
else
32+
{
33+
return BadRequest(ModelState);
34+
}
35+
}
36+
#endregion
37+
38+
[HttpPatch]
39+
public IActionResult JsonPatchWithModelStateAndPrefix(
40+
[FromBody] JsonPatchDocument<Customer> patchDoc,
41+
string prefix)
42+
{
43+
var customer = CreateCustomer();
44+
45+
patchDoc.ApplyTo(customer, ModelState, prefix);
46+
47+
if (!ModelState.IsValid)
48+
{
49+
return BadRequest(ModelState);
50+
}
51+
52+
return new ObjectResult(customer);
53+
}
54+
55+
[HttpPatch]
56+
public IActionResult JsonPatchWithoutModelState([FromBody] JsonPatchDocument<Customer> patchDoc)
57+
{
58+
var customer = CreateCustomer();
59+
60+
patchDoc.ApplyTo(customer);
61+
62+
return new ObjectResult(customer);
63+
}
64+
65+
[HttpPatch]
66+
public IActionResult JsonPatchForProduct([FromBody] JsonPatchDocument<Product> patchDoc)
67+
{
68+
var product = new Product();
69+
70+
patchDoc.ApplyTo(product);
71+
72+
return new ObjectResult(product);
73+
}
74+
75+
#region snippet_Dynamic
76+
[HttpPatch]
77+
public IActionResult JsonPatchForDynamic([FromBody]JsonPatchDocument patch)
78+
{
79+
dynamic obj = new ExpandoObject();
80+
patch.ApplyTo(obj);
81+
82+
return Ok(obj);
83+
}
84+
#endregion
85+
86+
private Customer CreateCustomer()
87+
{
88+
return new Customer
89+
{
90+
CustomerName = "John",
91+
Orders = new List<Order>()
92+
{
93+
new Order
94+
{
95+
OrderName = "Order0"
96+
},
97+
new Order
98+
{
99+
OrderName = "Order1"
100+
}
101+
}
102+
};
103+
}
104+
}
105+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp3.1</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.1" />
9+
</ItemGroup>
10+
11+
12+
</Project>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using Newtonsoft.Json;
2+
3+
namespace JsonPatchSample.Models
4+
{
5+
public class Category
6+
{
7+
public string CategoryName { get; set; }
8+
}
9+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System.Collections.Generic;
2+
3+
namespace JsonPatchSample.Models
4+
{
5+
public class Customer
6+
{
7+
public string CustomerName { get; set; }
8+
public List<Order> Orders { get; set; }
9+
}
10+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace JsonPatchSample.Models
2+
{
3+
public class Order
4+
{
5+
public string OrderName { get; set; }
6+
public string OrderType { get; set; }
7+
}
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace JsonPatchSample.Models
2+
{
3+
public class Product
4+
{
5+
public string ProductName { get; set; }
6+
public Category ProductCategory { get; set; }
7+
}
8+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using Microsoft.AspNetCore.Hosting;
2+
using Microsoft.Extensions.Hosting;
3+
4+
namespace JsonPatchSample
5+
{
6+
public class Program
7+
{
8+
public static void Main(string[] args)
9+
{
10+
CreateHostBuilder(args).Build().Run();
11+
}
12+
13+
public static IHostBuilder CreateHostBuilder(string[] args) =>
14+
Host.CreateDefaultBuilder(args)
15+
.ConfigureWebHostDefaults(webBuilder =>
16+
{
17+
webBuilder.UseStartup<Startup>();
18+
});
19+
}
20+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using Microsoft.AspNetCore.Builder;
2+
using Microsoft.AspNetCore.Hosting;
3+
using Microsoft.Extensions.Configuration;
4+
using Microsoft.Extensions.DependencyInjection;
5+
using Microsoft.Extensions.Hosting;
6+
7+
namespace JsonPatchSample
8+
{
9+
public class Startup
10+
{
11+
public Startup(IConfiguration configuration)
12+
{
13+
Configuration = configuration;
14+
}
15+
16+
public IConfiguration Configuration { get; }
17+
18+
// This method gets called by the runtime. Use this method to add services to the container.
19+
public void ConfigureServices(IServiceCollection services)
20+
{
21+
services.AddControllers()
22+
.AddNewtonsoftJson();
23+
}
24+
25+
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
26+
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
27+
{
28+
if (env.IsDevelopment())
29+
{
30+
app.UseDeveloperExceptionPage();
31+
}
32+
33+
app.UseHttpsRedirection();
34+
35+
app.UseRouting();
36+
37+
app.UseAuthorization();
38+
39+
app.UseEndpoints(endpoints =>
40+
{
41+
endpoints.MapControllers();
42+
});
43+
}
44+
}
45+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft": "Warning",
6+
"Microsoft.Hosting.Lifetime": "Information"
7+
}
8+
}
9+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft": "Warning",
6+
"Microsoft.Hosting.Lifetime": "Information"
7+
}
8+
},
9+
"AllowedHosts": "*"
10+
}

0 commit comments

Comments
 (0)