Tuesday, 21 March 2017

Swagger: How do I add custom return codes to my Web API?

Swagger is great tool to document APIs, The standard return codes (200 - OK) do not always give enough detail about the endpoint. Fortunately, there are some simple annotations that can be added to give a better description about the return code.

using Swashbuckle.Swagger.Annotations;
using System.Web.Http;

namespace MyNameSpace
{
    public class MyAPIController : ApiController
    {
        [HttpGet]
        [Route("api/my_custom_api_route")]
        [SwaggerResponse(System.Net.HttpStatusCode.OK, "All good here, thanks for asking")]
        [SwaggerResponse(601, "foo message")]
        public bool MyAPI()
        {
            return true;
        }
    }
}

The result is as follows:



No comments:

Post a comment