gpt4 book ai didi

asp.net - Asp.Core 中的模型绑定(bind)与继承

转载 作者:行者123 更新时间:2023-12-02 13:49:03 25 4
gpt4 key购买 nike

我需要接受用户的对象列表:

public async Task<IActionResult> CreateArticle(List<InformationBlockModel> informationBlocks)
{
...
}

ModelBinder 应该确定具体类型,但是当我尝试将 InformationBlock 转换为 TextInformationBlock 时,会抛出异常。

层次结构:

public class InformationBlockModel
{
public virtual InformationBlockType Type { get; set; }
}

public class TextInformationBlockModel : InformationBlockModel
{
public string Text { get; set; }

public override InformationBlockType Type { get; set; } = InformationBlockType.Text;
}

public class ImageInformationBlockModel : InformationBlockModel
{
public override InformationBlockType Type { get; set; } = InformationBlockType.Image;
public string Name { get; set; }
}

最佳答案

最后我找到了解决办法:

Startup.cs

services.AddMvc()
.AddJsonOptions(options => options.SerializerSettings.Converters.Add(new InformationBlockConverter()));

JsonCreationConverter.cs

public abstract class JsonCreationConverter<T> : JsonConverter
{
public override bool CanWrite { get; } = false;

public override bool CanRead { get; } = true;

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
}

protected abstract T Create(Type objectType, JObject jObject);

public override bool CanConvert(Type objectType)
{
return typeof(T) == objectType;
}

public override object ReadJson(JsonReader reader, Type objectType, object existingValue,
JsonSerializer serializer)
{
var jObject = JObject.Load(reader);

var target = Create(objectType, jObject);

serializer.Populate(jObject.CreateReader(), target);

return target;
}
}

信息 block 转换器

public class InformationBlockConverter : JsonCreationConverter<InformationBlockModel>
{
private readonly Dictionary<InformationBlockType, Type> _types = new Dictionary<InformationBlockType, Type>
{
{InformationBlockType.Text, typeof(TextInformationBlockModel)},
{InformationBlockType.Image, typeof(ImageInformationBlockModel)},
{InformationBlockType.Video, typeof(VideoInformationBlockModel)}
};

protected override InformationBlockModel Create(Type objectType, JObject jObject)
{
return (InformationBlockModel) jObject.ToObject(_types[Enum.Parse<InformationBlockType>(
jObject.GetValue("type", StringComparison.InvariantCultureIgnoreCase).Value<string>(), true)]);
}
}

信息 block 类型

public enum InformationBlockType
{
Text,
Image,
Video
}

关于asp.net - Asp.Core 中的模型绑定(bind)与继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47624747/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com