Do tnet Core 3.1 Kestrel Web API BadRequest, System.NotSupportedException - PullRequest
0 голосов
/ 10 февраля 2020

У меня на сервере есть функция, подобная этой

[HttpPost()]
public void Hello([FromBody]int Version)
{
}

Я смог вызвать вышеуказанный API, если опустить параметры [FromBody] int Version

var c = new HttpClient();
var Json = new StringContent(JsonConvert.SerializeObject(new { Version = 123}), Encoding.UTF8, "application/json")
var Resp = Api.PostAsync("http://MyURL", Json).Result;

Если [FromBody] int Версия включена. Я получаю следующую ошибку:

Request.Body
{Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpRequestStream}
    CanRead: true
    CanSeek: false
    CanTimeout: false
    CanWrite: false
    Length: '((Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpRequestStream)Request.Body).Length' threw an exception of type 'System.NotSupportedException'
    Position: '((Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpRequestStream)Request.Body).Position' threw an exception of type 'System.NotSupportedException'
    ReadTimeout: 'Request.Body.ReadTimeout' threw an exception of type 'System.InvalidOperationException'
    WriteTimeout: '((Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpRequestStream)Request.Body).WriteTimeout' threw an exception of type 'System.NotSupportedException'

1 Ответ

0 голосов
/ 10 февраля 2020

Я нашел ответ, в do tnet core 3.x.

  1. use Microsoft.AspNetCore. Mvc .Newtonsoft Json вместо Newtonsoft Json

  2. Добавить services.AddControllers (). AddNewtonsoft Json ();

, как указано в https://docs.microsoft.com/en-us/aspnet/core/migration/22-to-30?view=aspnetcore-2.2&tabs=visual-studio#jsonnet -поддержка

...