我的 API Controller 有一个如下所示的端点:
public async Task<IActionResult> Update([FromBody] UpdateCommand command) { /* ... */ }
那个command
看起来像这样:
public class UpdateCommand {
public string Username { get; set; }
public Id Id { get; set; } // <--- here's the problem
}
那个Id
是一个值对象,看起来像这样:
public class Id : SimpleValueObject<long> {
// base class has: IComparable, equality, GetHashCode, etc.
public Id(long value) : base(value) { }
public Id(string value) : base(Parse(value)) { }
private static long Parse(string value) { /* ... */ }
}
客户端会发送以下内容:
{
"username": "foo",
"id": 1
}
现在我希望模型绑定(bind)能够自动工作。但我很困惑如何做到这一点。
我实现了 IModelBinder
和IModelBinderProvider
,但这没有帮助。然后我注意到文档 say this :
Typically shouldn't be used to convert a string into a custom type, a TypeConverter is usually a better option.
所以我实现了 TypeConverter
,这也没有帮助。
然后我想实现一个JsonConverter<T>
,但该框架现在使用的不是 Newtonsoft,所以我没有走得太远。
所以我的问题是:我必须做什么才能促进自定义类型的自动绑定(bind)。我只需要知道要走哪条路,剩下的我会弄清楚。
(作为一个附带问题:请帮助我了解何时应该实现模型绑定(bind)器、类型转换器和 json 转换器。)
最佳答案
我仍然不明白何时使用自定义模型绑定(bind)器、自定义类型转换器和自定义 json 转换器。
但此场景的解决方案似乎是自定义的 JsonConverter<T>
.
这对我有用:
public class IdConverter : JsonConverter<Id> {
public override Id? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) {
if (reader.TokenType is not JsonTokenType.String and not JsonTokenType.Number)
throw new JsonException();
try {
if (reader.TokenType is JsonTokenType.String) {
var value = reader.GetString();
return new Id(value);
}
else {
var value = reader.GetInt64();
return new Id(value);
}
}
catch {
throw new JsonException();
}
}
public override void Write(Utf8JsonWriter writer, Id value, JsonSerializerOptions options) =>
writer.WriteNumberValue(value.Value);
}
关于c# - 使用哪种方法来模型绑定(bind)自定义类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68282576/