【发布时间】:2020-12-07 14:11:40
【问题描述】:
当找不到带有公司 ID 的公司时,我想返回 404 状态码。但是 Web API 总是返回;
"error": {
"message": "An error has occurred. Please, try it later.",
"issuedAt": "2020-08-18T08:06:19.4650733Z",
"code": "UnknownError",
"statusCode": 500
}
}
这是我的 CustomerProvider 代码;
public class CustomerProvider: ICustomerProvider
{
public CustomerProvider GetCustomerDetails()
{
//.....
//.....
if (customerDetails == null)
{
var resp = new HttpResponseMessage(HttpStatusCode.NotFound)
{
Content = new StringContent(string.Format("No customer with ID = {0}", customerId)),
ReasonPhrase = "Company ID Not Found"
};
throw new HttpResponseException(resp);
}
return customerDetails;
}
}
如何返回 404 状态码?
【问题讨论】:
标签: asp.net exception asp.net-web-api2 http-status-code-404