无法接收 POST 请求 Asp.net 核心 2.2

Can`t recive POST request Asp.net core 2.2

无法接收 3ds 派对 POST 请求。 415 不支持的媒体类型或空模型

在后端:Asp.net core 2.2 和 Aspnetbilerplate(如果它很重要)

来自第三方服务器的请求,所以我无法影响它。我只能指定请求将发送到的端点

看起来是这样的: curl http://MyServer/api/MyController/MyAction -d '{"a":"a", "b":1}'

我的代码 Dto:

    public class testDto
    {
        public string A { get; set; }
        public int B { get; set; }
    }

控制器:

[Route("api/[controller]/[action]")]
public class MyController : MyControllerBase
{
   ...
   [HttpPost]
   public async Task<testDto> MyAction(testDto dto)
   {
     //some code
     _logger.Info("test");            
   }
   ...
}

我从控制台发出的测试请求结果:

C:\WINDOWS\system32>curl -d '{"a":"a", "b":1}' http://myServerUrl/api/MyController/MyAction
curl: (3) [globbing] unmatched close brace/bracket in column 4
{"result":{"a":null,"b":0},"targetUrl":null,"success":true,"error":null,"unAuthorizedRequest":false,"__abp":true}

模型为空,未发生绑定。

我被添加 [FromBody] 到这样的动作:

   [HttpPost]
   public async Task<testDto> MyAction([FromBody]testDto dto)
   {
     //some code
     _logger.Info("test");            
   }

结果:HTTP 状态代码 415

另外,尝试添加 [FromForm] 和 [FromQyesry]。结果:空模型

有什么问题?我如何让它发挥作用? 提前谢谢你。

首先是请求curl http://MyServer/api/MyController/MyAction -d '{"a":"a", "b":1}'不正确,你可以看到协议使用:

curl http://MyServer/api/MyController/MyAction -d '{"a":"a", "b":1}' --trace-ascii debugdump.txt

如果查看转储文件,您会发现数据没有完全发送:

0000: POST /api/values/MyAction HTTP/1.1
0024: Host: localhost:44348
003b: User-Agent: curl/7.55.1
0054: Accept: */*
0061: Content-Length: 6
0074: Content-Type: application/x-www-form-urlencoded
00a5: 
=> Send data, 6 bytes (0x6)
0000: '{a:a,
== Info: upload completely sent off: 6 out of 6 bytes
== Info: schannel: client wants to read 102400 bytes
== Info: schannel: encdata_buffer resized 103424
== Info: schannel: encrypted data buffer: offset 0 length 103424
== Info: schannel: encrypted data got 322
== Info: schannel: encrypted data buffer: offset 322 length 103424

您应该与 3ds 方联系以确认请求。

无论如何,如果默认模型绑定不符合您的要求,您可以创建 Custom Model Binding :

  1. 正在添加中间件以发出请求EnableRewind :

    app.Use(async (ctx, next) =>
    {
        ctx.Request.EnableRewind();
        await next();
    });
    
  2. 创建实现 IModelBinder:

    的自定义活页夹
    public class testDtoEntityBinder : IModelBinder
    {
        public Task BindModelAsync(ModelBindingContext bindingContext)
        {
            if (bindingContext == null)
            {
                throw new ArgumentNullException(nameof(bindingContext));
            }
    
    
            var body = bindingContext.HttpContext.Request.Body;
            body.Position = 0;
    
    
            string raw = new System.IO.StreamReader(body).ReadToEnd();
    
            //now read content from request content and fill your model 
            var result = new testDto
            {
                A = "",
                B = 1,
            };
    
    
            bindingContext.Result = ModelBindingResult.Success(result);
            return Task.CompletedTask;
        }
    }
    
  3. 活页夹用户:

    [ModelBinder(BinderType = typeof(testDtoEntityBinder))]
    public class testDto
    {
        public string A { get; set; }
        public int B { get; set; }
    }