为同一端点使用任何类型的内容类型

No1*_*ver 5 c# asp.net-web-api .net-core asp.net-core

我有一个 asp.net core (v2.1) webapi 项目,它公开了此功能:

[HttpPost]
[Route("v1/do-something")]
public async Task<IActionResult> PostDoSomething(ModelData model)
{
    //...
}
Run Code Online (Sandbox Code Playgroud)

和这个模型:

public class ModelData
{
    [Required]
    public string Email { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

从内容类型的角度来看,我想让这个端点变得灵活。因此,在正文中发送此端点不同的内容类型应该可以。

例如,将允许那些“BODY”参数:

// application/x-www-form-urlencoded
email="abc123@gmail.com"

// application/json
{
    "email": "abc123@gmail.com"
}
Run Code Online (Sandbox Code Playgroud)

与旧的 .net 框架相比,在 dotnet core 中这是不允许开箱即用的。我发现我需要用属性添加Consume属性[FormForm]。但是,如果我将[FormForm]属性添加到模型参数中,它就不再适用于 JSON(例如)——因为那样它应该是[FromBody].

我认为使用这样的代码就可以了:

[HttpPost]
[Route("v1/do-something")]
public async Task<IActionResult> PostDoSomething([FromBody] [FromForm] ModelData model)
{
    //...
}
Run Code Online (Sandbox Code Playgroud)

但正如您所料,这段代码不起作用。

因此,为了实现这种灵活性,我必须复制所有端点 - 这听起来是一个非常非常糟糕的主意。

[HttpPost]
[Route("v1/do-something")]
[Consume ("application/json")]
public async Task<IActionResult> PostDoSomething([FromBody] ModelData model)
{
    //...
}

[HttpPost]
[Route("v1/do-something")]
[Consume ("application/x-www-form-urlencoded")]
public async Task<IActionResult> PostDoSomething([FromForm] ModelData model)
{
    //...
}

// ... Other content types here ...
Run Code Online (Sandbox Code Playgroud)

这听起来是一件容易的事。但好像比较复杂。

我错过了什么吗?如何使端点在任何内容类型中工作?

Sha*_*tin 5

这是一个根据内容类型进行绑定的自定义模型绑定器。

public class BodyOrForm : IModelBinder
{
    private readonly IModelBinderFactory factory;

    public BodyOrForm(IModelBinderFactory factory) => this.factory = factory;

    public async Task BindModelAsync(ModelBindingContext bindingContext)
    {
        var contentType = 
            bindingContext.ActionContext.HttpContext.Request.ContentType;

        BindingInfo bindingInfo = new BindingInfo();
        if (contentType == "application/json")
        {
            bindingInfo.BindingSource = BindingSource.Body;
        }
        else if (contentType == "application/x-www-form-urlencoded")
        {
            bindingInfo.BindingSource = BindingSource.Form;
        }
        else
        {
            bindingContext.Result = ModelBindingResult.Failed();
        }

        var binder = factory.CreateBinder(new ModelBinderFactoryContext
        {
            Metadata = bindingContext.ModelMetadata,
            BindingInfo = bindingInfo,
        });

        await binder.BindModelAsync(bindingContext);
    }
}
Run Code Online (Sandbox Code Playgroud)

通过以下操作进行测试。

[HttpPost]
[Route("api/body-or-form")]
public IActionResult PostDoSomething([ModelBinder(typeof(BodyOrForm))] ModelData model)
{
    return new OkObjectResult(model);
}
Run Code Online (Sandbox Code Playgroud)

这是GitHub 上的演示