在ASP.NET Core控制器操作中,是否有一种现成的方式将HTTP请求的整个主体绑定到字符串参数?

保罗·斯蒂格勒

概要

给定一个带有字符串主体“汉堡包”的HTTP请求,
我希望能够将请求的整个主体绑定到控制器动作的方法签名中的字符串参数。

通过向相对URL发出HTTP请求来调用此控制器时,string-body-model-binding-example/get-body出现错误,并且从未调用该操作

控制者

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace MyProject
{
    [Route("string-body-model-binding-example")]
    [ApiController]
    public class ExampleController: ControllerBase
    {
        [HttpPut("get-body")]
        public string GetRequestBody(string body)
        {
            return body;
        }
    }
}

证明问题的集成测试

using FluentAssertions;
using System;
using System.Net.Http;
using System.Threading.Tasks;
using Xunit;

public class MyIntegrationTests : MyIntegrationTestBase
{
    [Fact]
    public async Task String_body_is_bound_to_the_actions_body_parameter()
    {
        var body = "hamburger";
        var uri = "string-body-model-binding-example/get-body";
        var request = new HttpRequestMessage(HttpMethod.Put, uri)
        {
            Content = new StringContent(body, Encoding.UTF8, "text/plain")
        };

        var result = await HttpClient.SendAsync(request); 
        var responseBody = await result.Content.ReadAsStringAsync();
        responseBody.Should().Be(body,
            "The body should have been bound to the controller action's body parameter");
    }
}

注意:在上述示例中,使用Microsoft.AspNetCore.Mvc.Testing https://docs.microsoft.com/zh-cn/aspnet/core/test/integration-tests?view=aspnetcore-3.1设置了测试HttpClient 在动作方法签名中使用POCO模型的其他控制器动作是可以到达的,因此我知道我尝试进行模型绑定的方式有问题。

编辑:我尝试过的事情:

  • 将[FromBody]添加到参数=> 415不支持的媒体类型
  • 从控制器中删除[ApiController] =>命中了动作,但主体为null
  • 将[FromBody]添加到参数,然后从控制器中删除[ApiController] => 415不支持的媒体类型
  • 将[Consumes(“ text / plain”)]添加到不带[ApiController]和不带[FromBody]的操作中
  • 使用上述任何组合发送内容类型为application / json的请求=>错误或为空,具体取决于选项

令我惊讶的是,字符串不是受支持的原语之一

亚力山大

不确定是否可以通过框架手段实现,但是您可以为此创建自定义模型绑定器

public class RawBodyModelBinder : IModelBinder
{
    public async Task BindModelAsync(ModelBindingContext bindingContext)
    {
        using (var streamReader = new StreamReader(bindingContext.HttpContext.Request.Body))
        {
            string result = await streamReader.ReadToEndAsync();
            bindingContext.Result = ModelBindingResult.Success(result);
        }
    }
}

像这样使用

[HttpPut("get-body")]
public string GetRequestBody([ModelBinder(typeof(RawBodyModelBinder))] string body)
{
    return body;
}

或者,您可以使用告诉框架以更优雅的方式使用模型绑定程序IModelBinderProvider首先将newBindingSource作为单例引入

public static class CustomBindingSources
{
    public static BindingSource RawBody { get; } = new BindingSource("RawBod", "Raw Body", true, true);
}

并创建我们的[FromRawBody]属性

[AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public class FromRawBodyAttribute : Attribute, IBindingSourceMetadata
{
    public BindingSource BindingSource => CustomBindingSources.RawBody;
}

该框架IBindingSourceMetadata以特殊方式对待属性并BindingSource为我们获取其价值,因此可以在模型绑定器提供程序中使用。

然后创建 IModelBinderProvider

public class RawBodyModelBinderProvider : IModelBinderProvider
{
    public IModelBinder GetBinder(ModelBinderProviderContext context)
    {
        //use binder if parameter is string
        //and FromRawBody specified
        if (context.Metadata.ModelType == typeof(string) && 
            context.BindingInfo.BindingSource == CustomBindingSources.RawBody)
        {
            return new RawBodyModelBinder();
        }

        return null;
    }
}

在以下位置添加模型绑定程序提供程序 Startup

services
    .AddMvc(options =>
    {
        options.ModelBinderProviders.Insert(0, new RawBodyModelBinderProvider());
        //..
    }

使用它如下

[HttpPut("get-body")]
public string GetRequestBody([FromRawBody] string body)
{
    return body;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

Related 相关文章

热门标签

归档