Return “raw” json in ASP.NET Core 2.0 Web Api

后端 未结 2 609
臣服心动
臣服心动 2020-11-29 08:11

The standard way AFAIK to return data in ASP.NET Core Web Api is by using IActionResult and providing e.g. an OkObject result. This works fine with

相关标签:
2条回答
  • 2020-11-29 08:32

    And of course a few minutes after posting the question I stumble upon a solution :)

    Just return Content with the content type application/json...

    return Content("{ \"name\":\"John\", \"age\":31, \"city\":\"New York\" }", "application/json");
    
    0 讨论(0)
  • 2020-11-29 08:36

    In your action, replace Ok() with the Content() method, which lets you set the content (raw content), content type, and status code of your response: https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.contentresult?view=aspnetcore-2.0

    0 讨论(0)
提交回复
热议问题