AspNet Core WebApi中的自定义模型绑定?

问题描述 投票:0回答:1

每个人都有一个使用多态模型绑定的自定义模型绑定的有效示例吗?我正在尝试使用Web api项目的this example(适用于Mvc而非Api项目),但不适用于API项目。我认为在填充ValueProvider方面缺少一些步骤,但是我找不到与此相关的任何资源(AspNet Core 3.1)。

我到目前为止的尝试:

Dtos:

public abstract class Device
{
    public string Kind { get; set; }
}

public class Laptop : Device
{
    public string CPUIndex { get; set; }
}

public class SmartPhone : Device
{
    public string ScreenSize { get; set; }
}

自定义模型活页夹实现:

public class DeviceModelBinderProvider : IModelBinderProvider
{
    public IModelBinder GetBinder(ModelBinderProviderContext context)
    {
        if (context.Metadata.ModelType != typeof(Device))
        {
            return null;
        }

        var subclasses = new[] { typeof(Laptop), typeof(SmartPhone), };

        var binders = new Dictionary<Type, (ModelMetadata, IModelBinder)>();
        foreach (var type in subclasses)
        {
            var modelMetadata = context.MetadataProvider.GetMetadataForType(type);
            binders[type] = (modelMetadata, context.CreateBinder(modelMetadata));
        }

        return new DeviceModelBinder(binders);
    }
}

public class DeviceModelBinder : IModelBinder
{
    private Dictionary<Type, (ModelMetadata, IModelBinder)> binders;

    public DeviceModelBinder(Dictionary<Type, (ModelMetadata, IModelBinder)> binders)
    {
        this.binders = binders;
    }

    public async Task BindModelAsync(ModelBindingContext bindingContext)
    {
        var modelKindName = ModelNames.CreatePropertyModelName(bindingContext.ModelName, nameof(Device.Kind));
        var modelTypeValue = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);

        IModelBinder modelBinder;
        ModelMetadata modelMetadata;
        if (modelTypeValue.FirstValue == "Laptop")
        {
            (modelMetadata, modelBinder) = binders[typeof(Laptop)];
        }
        else if (modelTypeValue.FirstValue == "SmartPhone")
        {
            (modelMetadata, modelBinder) = binders[typeof(SmartPhone)];
        }
        else
        {
            bindingContext.Result = ModelBindingResult.Failed();
            return;
        }

        var newBindingContext = DefaultModelBindingContext.CreateBindingContext(
            bindingContext.ActionContext,
            bindingContext.ValueProvider,
            modelMetadata,
            bindingInfo: null,
            bindingContext.ModelName);

        await modelBinder.BindModelAsync(newBindingContext);
        bindingContext.Result = newBindingContext.Result;

        if (newBindingContext.Result.IsModelSet)
        {
            // Setting the ValidationState ensures properties on derived types are correctly 
            bindingContext.ValidationState[newBindingContext.Result] = new ValidationStateEntry
            {
                Metadata = modelMetadata,
            };
        }
    }
}

我像这样注册模型绑定程序提供程序:

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllers(o => o.ModelBinderProviders.Insert(0, new DeviceModelBinderProvider()));
    }

然后是我的控制器:

[ApiController]
[Route("test")]
public class TestController : ControllerBase
{
    [HttpPost]
    public IActionResult Test(Device dto)
    {
        var x = dto;
        return Ok();
    }
}

我正在发布一个json请求正文,例如:

{
    "ScreenSize": "1",
    "Kind": "SmartPhone"
}

由于存在太多的不可思议之处,因此真的对文档感到厌烦。我的后备方法是从请求中手动解析HttpContent并反序列化。但我希望像示例中一样使用模型绑定器方法。我看到的唯一两件事是,bindingContext.ModelName为空,bindingContext.ValueProvider仅具有包含actioncontroller键的路由值提供程序。因此,似乎主体甚至都没有解析为值提供者。

c# asp.net-core model-binding custom-model-binder
1个回答
0
投票

我已经尝试过与您发布的代码完全相同,并且对我有用。

这里是其值的图像。

enter image description here

这是邮递员请求的屏幕截图。

enter image description here

邮递员的CURL请求。

curl --location --request POST 'https://localhost:44332/test' \
--header 'Content-Type: application/json' \
--form 'ScreenSize=1' \
--form 'Kind=SmartPhone'

startup.cs如下图所示。enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.