Kir*_*ykh 8 c# json system.text.json asp.net-core-5.0
任务:
我将利用 asp.net core 5.0 为第三方推送通知提供端点。推送通知有不同的类型和模式。这些通知需要放入 mysql 数据库的 json 列中,并读取一些值来创建内部事件并将其放入队列中。所以我想将请求正文绑定到一个“json 对象”,我可以从中读取值,然后按原样保存在数据库中。
选择问题:
Asp.net core 5.0 使用System.Text.Json. System.Text.Json提供类型JsonDocument,根据文档应将其处置,以及JsonElement。绑定机制允许将请求正文绑定到JsonDocument和JsonElement:
[HttpPost]
public async Task<ActionResult> ProcessNotificationAsync(
[FromBody] JsonDocument notification,
[FromServices] IProcessNotificationCommand processCommand)
{
await processCommand.ProcessAsync(notification);
return NoContent();
}
Run Code Online (Sandbox Code Playgroud)
[HttpPost]
public async Task<ActionResult> ProcessNotificationAsync(
[FromBody] JsonElement notification,
[FromServices] IProcessNotificationCommand processCommand)
{
await processCommand.ProcessAsync(notification);
return NoContent();
}
Run Code Online (Sandbox Code Playgroud)
哪种方法可以安全使用、绑定JsonDocument或绑定JsonElement?
如果我使用绑定到 JsonDocument ,绑定机制会在处理请求时处理 JsonDocument 吗?或者需要我编码?
[HttpPost]
public async Task<ActionResult> ProcessNotificationAsync(
[FromBody] JsonDocument notification,
[FromServices] IProcessNotificationCommand processCommand)
{
using (notification)
{
await processCommand.ProcessAsync(notification);
}
return NoContent();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2888 次 |
| 最近记录: |