我正在寻找在我的.NET Core Web API控制器中使用HTTP状态代码返回JSON的正确方法.我用它像这样:
public IHttpActionResult GetResourceData()
{
return this.Content(HttpStatusCode.OK, new { response = "Hello"});
}
Run Code Online (Sandbox Code Playgroud)
这是一个4.6 MVC应用程序,但现在随着.NET核心我似乎没有这个IHttpActionResult我已经ActionResult和使用这样的:
public ActionResult IsAuthenticated()
{
return Ok(Json("123"));
}
Run Code Online (Sandbox Code Playgroud)
但是来自服务器的响应很奇怪,如下图所示:
我只是希望Web API控制器返回带有HTTP状态代码的JSON,就像我在Web API 2中所做的那样.
鉴于代码:
dynamic foo = new ExpandoObject();
foo.Bar = "something";
string json = Newtonsoft.Json.JsonConvert.SerializeObject(foo);
Run Code Online (Sandbox Code Playgroud)
输出如下:
"{\"Bar\":\"something\"}"
Run Code Online (Sandbox Code Playgroud)
在调试大型json文档时,很难阅读 - 使用Newtonsoft.Json的内置功能(不是正则表达式或可能破坏事物的黑客)有没有办法使输出成为一个带有valie的字符串:
{Bar: "something"}
Run Code Online (Sandbox Code Playgroud)