ASP.NET Core 返回帶有狀態代碼的 JSON - ASP.NET Core return JSON with status code

問題:

I'm looking for the correct way to return JSON with a HTTP status code in my .NET Core Web API controller.我正在尋找在我的 .NET Core Web API 控制器中返回帶有 HTTP 狀態代碼的 JSON 的正確方法。 I use to use it like this:我習慣這樣使用它:

public IHttpActionResult GetResourceData()
{
    return this.Content(HttpStatusCode.OK, new { response = "Hello"});
}

This was in a 4.6 MVC application but now with .NET Core I don't seem to have this IHttpActionResult I have ActionResult and using like this:這是在 4.6 MVC 應用程序中,但現在使用 .NET Core 我似乎沒有這個IHttpActionResult我有ActionResult並使用如下:

public ActionResult IsAuthenticated()
{
    return Ok(Json("123"));
}

But the response from the server is weird, as in the image below:但是服務器的響應很奇怪,如下圖所示:

在此處輸入圖片說明

I just want the Web API controller to return JSON with a HTTP status code like I did in Web API 2.我只希望 Web API 控制器像我在 Web API 2 中所做的那樣返回帶有 HTTP 狀態代碼的 JSON。


解決方案:

參考一: https://en.stackoom.com/question/2rjo3
參考二: https://stackoom.com/question/2rjo3
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章