簡體   English   中英

ASP.NET 核心 - 如何從我的 C# class 到我的 Z558B5744CF685F39D34EZ906B 客戶端應用程序中獲取我的狀態代碼?

[英]ASP.NET Core - How do I get my status code from my C# class to my TypeScript Client App?

我有這個名為 ProjectDecommissionRequestController 的 controller。

我有這個 TypeScript 客戶端應用程序,它基本上根據狀態代碼打印某個字符串(例如,在狀態代碼 500 的情況下給出一般錯誤)。

const response = await fetch(`api/projectdecommissionrequest`, {
    body: JSON.stringify(postBody),
    headers: new Headers({
        "Accept": "application/json",
        "Authorization": `Bearer ${token}`,
        "Content-Type": "application/json",
    }),
    method: "POST",
});
console.log(response.status);

我的 controller 調用另一個 class 執行此檢查並引發錯誤 -

 if (!repoUrlFormatMatch.Success)            
                throw new FormatException($"The repositoryUri '{repositoryUri}' did not conform to the expected format for an Azure Devops Git repository.");

我想將此異常的狀態代碼設置為 500 以外的值,以便我可以編寫更具體的錯誤。

任何幫助將不勝感激。 謝謝!

您可以使用StatusCode方法:

        public ActionResult MyMethodWithStatusCode(int code)
        {
            return StatusCode(code);
        }

常用代碼也有快捷方式: BadRequest NotFound等。

暫無
暫無

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

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