[英]ASP.NET Core - How do I get my status code from my C# class to my TypeScript Client App?
I have this controller called ProjectDecommissionRequestController.我有这个名为 ProjectDecommissionRequestController 的 controller。
And I have this TypeScript client app which basically prints a certain string according to the status code (for eg gives a generic error in case of status code 500).我有这个 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);
My controller calls another class which performs this check and throws an error -我的 controller 调用另一个 class 执行此检查并引发错误 -
if (!repoUrlFormatMatch.Success)
throw new FormatException($"The repositoryUri '{repositoryUri}' did not conform to the expected format for an Azure Devops Git repository.");
I want to set the status code for this exception to be something other than 500 so that I can write a more specific error.我想将此异常的状态代码设置为 500 以外的值,以便我可以编写更具体的错误。
Any help will be appreciated.任何帮助将不胜感激。 Thank you!
谢谢!
You can use StatusCode
method:您可以使用
StatusCode
方法:
public ActionResult MyMethodWithStatusCode(int code)
{
return StatusCode(code);
}
There are also shortcut methods for commonly used codes: BadRequest NotFound etc.常用代码也有快捷方式: BadRequest NotFound等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.