我如何返回 HTTP 403
来自 WebAPI
方法?我试过扔 HttpResponseException
与 HttpStatusCode.Forbidden
,我试过了
return request.CreateErrorResponse(HttpStatusCode.Forbidden, pEx);
两者都不起作用。两者总是返回
HTTP 200
.我错过了什么?它必须是一些简单的东西,但我没有看到。
最佳答案
您的路由配置可能有问题。下面是一个工作示例。把它放在你的 Controller 中,看看它是否有效。如果没有,请使用诊断工具(即 Cobisi Routing Assistant)检查您的路由。
public HttpResponseMessage GetSomeString(int id)
{
// This method is not allowed!
return this.Request.CreateErrorResponse(HttpStatusCode.Forbidden, "This method is not allowed!");
}
关于asp.net - 在 Web API 方法中返回 HTTP 403,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17201255/