Returning HTTP 403 in a WebAPI method

后端 未结 1 363
说谎
说谎 2021-01-17 15:22

How do I return an HTTP 403 from a WebAPI method? I\'ve tried throwing an HttpResponseException with HttpStatusCode.Forbidden

1条回答
  •  一整个雨季
    2021-01-17 16:22

    You might have a problem with your routing configuration. Below is a working sample. Put it in your controller and see if it works. If it doesn't, check your routing with a diagnostic tool (i.e. Cobisi Routing Assistant).

    public HttpResponseMessage GetSomeString(int id)
    {
        // This method is not allowed!
        return this.Request.CreateErrorResponse(HttpStatusCode.Forbidden, "This method is not allowed!");
    }
    

    0 讨论(0)
提交回复
热议问题