我在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] 删除。
我来说两句