使用POST请求将WebApi序列化为Json

使用POST请求将WebApi序列化为Json,json,asp.net-core,serialization,swagger,Json,Asp.net Core,Serialization,Swagger,我有以下WebApi控制器 [Route("api/[controller]")] public class FunctionController : ControllerBase { private readonly ILogger<FunctionController> _logger; private readonly IServiceAccessor<IFunctionManagementService> _functionManagementSe

我有以下WebApi控制器

[Route("api/[controller]")]
public class FunctionController : ControllerBase
{
    private readonly ILogger<FunctionController> _logger;
    private readonly IServiceAccessor<IFunctionManagementService> _functionManagementService;

    public FunctionController(
        IServiceAccessor<IFunctionManagementService> FunctionManagementService,
        ILogger<FunctionController> logger)
    {
        _functionManagementService = FunctionManagementService;
        _logger = logger;
    }

    [HttpPost]
    [SwaggerOperation(nameof(RegisterFunction))]
    [SwaggerResponse(StatusCodes.Status200OK, "OK", typeof(FunctionRegisteredResponseDto))]
    [SwaggerResponse(StatusCodes.Status400BadRequest, "Bad Request")]
    public async Task<IActionResult> RegisterFunction(RegisterFunctionDto rsd)
    {
        var registeredResponse = await _functionManagementService.Service.RegisterFunctionAsync(rsd);
        if (registeredResponse.Id > -1)
            return Ok(registeredResponse);

        return BadRequest(registeredResponse);
    }

    [HttpDelete("{id}")]
    [SwaggerOperation(nameof(UnregisterFunction))]
    [SwaggerResponse(StatusCodes.Status200OK, "OK")]
    [SwaggerResponse(StatusCodes.Status404NotFound, "Not Found")]
    [SwaggerResponse(StatusCodes.Status400BadRequest, "Bad Request")]
    public async Task<IActionResult> UnregisterFunction(string sid)
    {
        if (!long.TryParse(sid, out long id))
            return new BadRequestObjectResult(new { message = "400 Bad Request", UnknownId = sid });

        if (!await _functionManagementService.Service.UnregisterFunctionAsync(id))
            return new NotFoundObjectResult(new { message = "404 Not Found", UnknownId = sid });

        return new OkObjectResult(new { Message = "200 OK", Id = id, Unregistered = true });
    }
}

问:如何使用Newtonsoft.Json和post-to-me服务正确序列化对象?

如果使用
HttpClient.PostAsync
,则无需创建
HttpRequestMessage
。只需构建内容并发送即可

RegisterFunctionDto rsd = Utils.GetRegisterFunctionDtoObject();
string serializedDto = JsonConvert.SerializeObject(rsd);
var content = new StringContent(serializedDto, Encoding.UTF8, "application/json");    

HttpResponseMessage response = await client.PostAsync("api/Function", content);
您还可以明确地告诉操作绑定到请求体中的数据

//...
public async Task<IActionResult> RegisterFunction([FromBody]RegisterFunctionDto rsd) {
    //...
}
/。。。
公共异步任务注册表函数([FromBody]注册表函数到rsd){
//...
}
RegisterFunctionDto rsd = Utils.GetRegisterFunctionDtoObject();
string serializedDto = JsonConvert.SerializeObject(rsd);
var content = new StringContent(serializedDto, Encoding.UTF8, "application/json");    

HttpResponseMessage response = await client.PostAsync("api/Function", content);
//...
public async Task<IActionResult> RegisterFunction([FromBody]RegisterFunctionDto rsd) {
    //...
}