c# - 自定义模型绑定 - ASP.NET Core
问题描述
我有这个带有这个有效载荷的传入请求
{
count : 10,
supplier : {
id : 342,
name : 'test'
},
title : 'some title'
}
我的 C# 代码中有这个模型
class SomeModel
{
public string Title { get; set; }
public int SupplierId { get; set; }
public double Amount { get; set; }
}
这是我的控制器方法
public IActionResult Foo(SomeModel data)
{
//...
}
我想将count
请求有效负载中的Amount
属性映射到我的 c# 模型中的属性并将值映射supplier.id
到SupplierId
.
我正在使用 Newtonsoft Json.NET 库进行绑定
解决方案
显然,最简单的方法是像这样创建一个与有效载荷结构相对应的类
public class SomeModel
{
public string Title { get; set; }
public double Count { get; set; }
public Supplier Supplier { get; set; }
}
public class Supplier
{
public int Id { get; set; }
public string Name { get; set; }
}
另一个迭代可能是使用JsonProperty
属性 forAmount
和SupplierId
属性利用Supplier
class SomeModel
{
public string Title { get; set; }
[JsonProperty("count")]
public double Amount { get; set; }
public int SupplierId => Supplier.Id;
public Supplier Supplier { get; set; }
}
class Supplier
{
public int Id { get; set; }
public string Name { get; set; }
}
但是,如果您想坚持使用当前模型,则需要创建一个自定义转换器。我能建议你什么
public class NestedPropertyConverter : JsonConverter
{
private string[] _properties;
public NestedPropertyConverter(string propertyChain)
{
//todo: check if property chain has valid structure
_properties = propertyChain.Split('.');
}
public override bool CanWrite => false;
public override bool CanConvert(Type objectType) => true;
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
JToken token = (JToken)serializer.Deserialize(reader);
foreach (string property in _properties)
{
token = token[property];
if (token == null) //if property doesn't exist
return existingValue; //or throw exception
}
return token.ToObject(objectType);
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
基本上,转换器允许绑定嵌套属性。用法
class SomeModel
{
public string Title { get; set; }
[JsonConverter(typeof(NestedPropertyConverter), "id")]
[JsonProperty("supplier")] //its necessary to specify top level property name
public int SupplierId { get; set; }
[JsonProperty("count")]
public double Amount { get; set; }
}
笔记
我还测试了以下有效载荷
{
count : 10,
supplier : {
id : 342,
name : "test",
test: {
val: 55
}
},
title : "some title"
}
和配置属性
[JsonConverter(typeof(NestedPropertyConverter), "test.val")]
[JsonProperty("supplier")]
public int SupplierId { get; set; }
它工作正常。
推荐阅读
- bash - 在 if 语句 bash 中调用函数
- kubernetes - 错误:选择器与模板标签不匹配
- javascript - 如何在执行之前捕获 Echarts Toolbox 事件,如 saveAsImage
- javascript - 绑定到 html 的角度推送和拼接数组元素
- python - Python3 cx-Oracle Oracle 客户端库无法加载:Linux 上的“libclntsh.so”
- c# - 如何在剃须刀的表格中添加一些页面
- python - Tensorflow - 迭代两个张量
- oracle - 需要帮助才能完全删除临时表 oracle 程序 - Oracle - PL/SQL
- string - 比较bash脚本中的字符串
- python - 是否可以(快速)在networkx图中仅找到第一个循环?