簡體   English   中英

如何從 Razor 頁面返回帶有 HTTP 狀態代碼 500 的 JsonResult?

[英]How to return JsonResult with HTTP Status Code 500 from Razor Page?

在我的 ASP.NET Core Razor Pages 應用程序中,我有時會通過 ajax 向服務器發送請求。

在這些情況下,我想為成功或錯誤返回JsonResult

但是我找不到更改 JsonResult 的JsonResult狀態代碼的方法。

這是我的方法:


public IActionResult OnPostFileUpload()
{
   try
   {
       // code to manage and save the file, this request is AJAX
       return new JsonResult("done");
   }
   catch (Exception ex)
   {
       return new JsonResult(message); // how should I set it to be 500?
   }
}

您可以將 StatusCode 分配給JsonResult ,如下所示:

using System.Net;
return new JsonResult(message)
{
    StatusCode = (int)HttpStatusCode.InternalServerError
};

參考

JsonResult class 屬性

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM