8

In previous versions of ASP.NET, when I created Web Api 2, visual studio automatically wired up automatic generation of documentation for the API.

It's also explained here: http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/creating-api-help-pages

I'm writing a new project and I've decided to do it with ASP.NET Core, but I don't see anything similar to what existed in the previous versions that generates the documentation from the API (I'm also guessing that's its a bit different since all controllers now inherit from the same Controller class).

But, is there some way to have help files generated for ASP.NET Core APIs?

2 Answers 2

2

Well, modern way is to use Swashbuckle NuGet Package:

dotnet add package Swashbuckle.AspNetCore

It takes care about generating JSON file with documentation of your .NET API.

In code you need to make following adjustments:

builder.Services.AddSwaggerGen(options =>
{
    // Optional: Include XML comments
    var xmlFilename = $"{System.Reflection.Assembly.GetExecutingAssembly().GetName().Name}.xml";
    options.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, xmlFilename));
});

var app = builder.Build();

// Enable Swagger middleware
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI(c =>
    {
        c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
        c.RoutePrefix = string.Empty; // Serve Swagger UI at app root
    });
}

Now you are able to go to swagger page /swagger and access also underlaying JSON documentation file /swagger/v1/swagger.json.

Also, for documentation generation there is also Microsoft package that can be used for that. Please take a look at Generate OpenAPI documents

To use it you need nuget package

dotnet add package Microsoft.AspNetCore.OpenApi

with following changes in code:

builder.Services.AddOpenApi();

var app = builder.Build();

if (app.Environment.IsDevelopment())
{
    app.MapOpenApi();
    app.UseSwaggerUI(options => options.SwaggerEndpoint("/openapi/v1.json", "Open API"))
}

Then, in swagger UI you need to add path to Open API docs

References: Get started with Swashbuckle and ASP.NET Core

Sign up to request clarification or add additional context in comments.

Comments

1

For this instance, I used the Microsoft.AspNetCore.Mvc.ApiExplorer to inject IApiDescriptionGroupCollectionProvider, and tryaddenumerable for IApiDescriptionProvider, DefaultApiDescriptionProvider

I then created a help controller which provided me a list of string from looping into ApiDescriptionGroups.Items[0] then getting each relativepath. Also checking if there are ParameterDescriptions then getting all its name to stringbuilder to provide me the API description. Not bad but hehe. That's what the old man wants me todo. Hahaha.

EDIT: You can discard the tryaddenumerable for IApiDescriptionProvider, DefaultApiDescriptionProvider as for this example i am not using the provider to be expanded in the api documentation entities.

2 Comments

Interesting. Can you provide some example codes of what you did?
As of now I can't provide my code as it go against our company standards. By the way the tryaddenumerable dependency can be discarded for this solution as the provider itself is very useful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.