Search code examples
asp.net-web-apidocumentationasp.net-web-api2documentation-generationasp.net-web-api-helppages

WebAPI - auto generate documentation with all possible responses


I have read an article http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/creating-api-help-pages and it works fine. But I would prefer to add all possible responses and conditions, when these responses will be sent. Any tool to analyze each WebAPI method, found all places with responses, why it can be happened and create a documentation about it automatically?

I.e. I have the following code:

    public HttpResponseMessage GetEditorialRequests()
    {
        SetUser();
        try
        {
            var r_list = _service.RequestList(user.Id);
            if (r_list.Count > 0)
            {
                var list = mapper.Map<List<SmartphonePhotographerRequestElementApiModel>>(r_list);
                return Request.CreateResponse<List<SmartphonePhotographerRequestElementApiModel>>(HttpStatusCode.OK, list);
            }
            else
                return Request.CreateResponse(HttpStatusCode.NoContent);
        }
        catch (PixlocateBusinessLogic.NoSmartphonePhotographerLocationException)
        {
            return Request.CreateErrorResponse(HttpStatusCode.BadRequest, new HttpError("User does not have any locations") { { "CustomStatusCode", 466 } }); 
        }
    }

I would like to have documentation, which describes that method returns:

  1. StatusCode = 200 and list of elements when method is completed successfully and elements are found
  2. StatusCode = 204 when method is completed successfully and no elements found
  3. StatusCode = 400 when location is not found (condition for getting editorial requests) and detail response with message and CustomStatusCode

Solution

  • Have you tried Swagger (and Swashbuckle)?

    Swagger

    Swagger is a simple yet powerful representation of your RESTful API. With the largest ecosystem of API tooling on the planet, thousands of developers are supporting Swagger in almost every modern programming language and deployment environment. With a Swagger-enabled API, you get interactive documentation, client SDK generation and discoverability.

    Swashbuckle

    Seamlessly adds a Swagger to WebApi projects! Combines ApiExplorer and Swagger/swagger-ui to provide a rich discovery, documentation and playground experience to your API consumers. In addition to its Swagger generator, Swashbuckle also contains an embedded version of swagger-ui which it will automatically serve up once Swashbuckle is installed. This means you can complement your API with a slick discovery UI to assist consumers with their integration efforts. Best of all, it requires minimal coding and maintenance, allowing you to focus on building an awesome API!

    I use them in many projects, very easy to use and extremely powerful.