Marcus H Marcus H - 1 month ago 5
C# Question

Which implementations of IHttpActionResult exist

I have identified these pre-defined responses:

Ok - 200
NotFound - 404
Exception - 500
Unauthorized - 401
BadRequest - 400
Conflict
Redirect
InvalidModelState


But I can't find any docs about the total range of helper methods that exist..Are there any more?

I've tried Microsoft site and only found these docs but they don't list them at all..

Help appreciated

Answer

If you select ApiController and press F12 you will see the definition of it. There are listed all the helpers.

enter image description here

If you want to create a custom one here is an example:

public class ForbiddenResult : IHttpActionResult
{
    private readonly HttpRequestMessage _request;
    private readonly string _reason;

    public ForbiddenResult(HttpRequestMessage request,string reason)
    {
        _request = request;
        _reason = reason;
    }

    public ForbiddenResult(HttpRequestMessage request)
    {
        _request = request;
        _reason = "Forbidden";
    }

    public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
    {
        var response = _request.CreateResponse(HttpStatusCode.Forbidden,_reason);
        return Task.FromResult(response);
    }
}