gal*_*ets 2 asp.net-mvc asp.net-mvc-4
我有一个函数ApiController,它应该输出某种对象.该函数以已经序列化的格式接收该对象.为了使函数最终能够恢复有效的JSON,我需要将其转换为JObject,如下所示:
[HttpGet]
public object MyFunction()
{
string result = .......;
return JObject.Parse(result);
}
Run Code Online (Sandbox Code Playgroud)
问题是:结果已经是一个有效的JSON,我希望按原样返回它,而不需要再处理.如果我的班级是常规的Controller,我本可以刚刚退回一个ContentResult,但由于某些原因,我不能在这种情况下ApiController变成常规Controller.
还有其他选择吗?
Luk*_*ton 10
如果操作返回System.Net.Http.HttpResponseMessage,则ASP.NET Web API将返回值直接转换为HTTP响应消息,使用HttpResponseMessage对象的属性来填充响应.
此选项使您可以对响应消息进行大量控制.
返回原始json的示例用法:( 参考 /sf/answers/1196854361/)
public HttpResponseMessage Get()
{
string yourJson = GetJsonFromSomewhere();
var response = Request.CreateResponse(HttpStatusCode.OK);
response.Content = new StringContent(yourJson, Encoding.UTF8, "application/json");
return response;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8257 次 |
| 最近记录: |