我该如何在WebAPI
方法中返回HTTP 403
? 我已尝试使用HttpStatusCode.Forbidden
抛出HttpResponseException
,但似乎无效。
return request.CreateErrorResponse(HttpStatusCode.Forbidden, pEx);
两者都不起作用。它们总是返回一个 HTTP 200
。我错过了什么?一定是一些简单的东西,但我没有看到。
我该如何在WebAPI
方法中返回HTTP 403
? 我已尝试使用HttpStatusCode.Forbidden
抛出HttpResponseException
,但似乎无效。
return request.CreateErrorResponse(HttpStatusCode.Forbidden, pEx);
两者都不起作用。它们总是返回一个 HTTP 200
。我错过了什么?一定是一些简单的东西,但我没有看到。
您可能存在路由配置问题。以下是一个可用的示例,请将其放在您的控制器中并查看是否有效。如果无效,请使用诊断工具(例如Cobisi Routing Assistant)检查您的路由配置。
public HttpResponseMessage GetSomeString(int id)
{
// This method is not allowed!
return this.Request.CreateErrorResponse(HttpStatusCode.Forbidden, "This method is not allowed!");
}