ASP.NET Core 3.1 的 GET 方法体会破坏请求。

26 浏览
0 Comments

ASP.NET Core 3.1 的 GET 方法体会破坏请求。

这个问题已经有了答案:

HTTP GET with request body

在ASP.NET Core 3.1 Web API控制器上发生了问题。我在控制器中有一个方法,具有[HttpGet]属性和一个[FromBody]参数:

[HttpGet]
public async Task GetMessages([FromBody] MessageRM messagesRequest)

这个方法期望接收一个请求模型:

public class MessageRM
{
    public int RegionID { get; set; }
    public int? LastMessageID { get; set; }
    public int? StartMessageID { get; set; }
    public int? PageSize { get; set; }
}

当我对没有请求体的这个方法进行get请求时,返回的结果是:

{"type":"https://tools.ietf.org/html/rfc7231#section-6.5.13","title":"Unsupported Media Type","status":415,"traceId":"|f6d2c92d-47865e9e3ad9a87d."}

这非常出乎意料。我感觉自己缺少了重要的东西,当我传递请求体时,这种感觉增强了。首先是使用\'{}\',然后完全模拟我的请求对象。这些请求中的任何一个都包含了\'application/json\'内容类型头。两次都是同样的响应:

{"type":"https://tools.ietf.org/html/rfc7231#section-6.5.1","title":"One or more validation errors occurred.","status":400,"traceId":"|f6d2c92e-47865e9e3ad9a87d.","errors":{"$":["The input does not contain any JSON tokens. Expected the input to start with a valid JSON token, when isFinalBlock is true. Path: $ | LineNumber: 0 | BytePositionInLine: 0."]}}

有什么建议吗?

admin 更改状态以发布 2023年5月23日
0
0 Comments

将[HttpGet]改为[HttpPost],发出POST请求而不是GET请求。

0