![](/img/trans.png)
[英]Reusing code from my ASP.NET Core MVC app in console app : how to do database connection
[英]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.