如何避免Web API中的JSON反序列化/序列化?

奥拉夫

我在ASP.NET 2.0的Web API控制器中有以下代码:

[HttpGet]
[Route("{controllerName}/{nodeId}/ConfigurationValues")]
public async Task<IActionResult> GetConfigurationValues(string controllerName, byte nodeId, string code)
{
    string payload = ...

    HttpResponseMessage response = await deviceControllerRepository.ExecuteMethodAsync(controllerName, "GetNodeConfigurationValues", payload);

    string responseJson = await response.Content.ReadAsStringAsync();
    var configurationValues = JsonConvert.DeserializeObject<List<ConfigurationValue>>(responseJson);

    return Ok(configurationValues);
}

我如何避免在将responseJson以正确的JSON格式返回之前将其反序列化为.NET对象?

我试图将方法更改为返回HttpResponseMessage,但这导致将不正确的JSON传递回调用方。

代码丰满

ControllerBase类具有Content()用于此目的方法。确保在输出数据包中设置正确的Content-Type标头:

return Content(responseJson, "application/json");

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

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

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章