4

I'd like to return Http 403 errors from my Asp.Net WebApi controllers when the user does not have permission to perform certain tasks.

However, I'd like to use a substatus on this to give further details about the error, along with the error message.

At the moment, what I get is

HTTP/1.1 403 Read access forbidden

but what I'd like to see is

HTTP/1.1 403.2 Read access forbidden

The code I'm using currently:

    [HttpGet]
    public EnrollmentDetail Details(int id)
    {
        var enrollmentDetail = _context.GetEnrollmentDetail(id);

        if (!enrollmentDetail.R)
        {
            throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Forbidden)
                {
                    ReasonPhrase = "Read access forbidden"
                });
        }

        return enrollmentDetail;
    }

I can't find any information any where on how to add these sub-statuses to the response. Is there any way it can be done with the built-in classes? If not, is there a way to write a custom HttpException which could do this for me?

a_m0d
  • 12,034
  • 15
  • 57
  • 79
  • I can't find an exact answer but I think these two links should help. http://www.iis.net/learn/troubleshoot/diagnosing-http-errors/how-to-use-http-detailed-errors-in-iis http://msdn.microsoft.com/en-us/library/system.web.httpresponse.substatuscode.aspx – Bruce Burge Jun 21 '13 at 19:48

1 Answers1

0

That's because sub-statuses are not part of the HTTP spec and should not be used. If you want to send more details about the problem you encountered, take a look at Json-problem

Community
  • 1
  • 1
Darrel Miller
  • 139,164
  • 32
  • 194
  • 243