3.4 KiB
title | author | description | monikerRange | ms.author | ms.custom | ms.date | uid |
---|---|---|---|---|---|---|---|
Use web API analyzers | pranavkm | Learn about the web API analyzers in Microsoft.AspNetCore.Mvc.Api.Analyzers. | >= aspnetcore-2.2 | pranavkm | mvc | 12/14/2018 | web-api/advanced/analyzers |
Use web API analyzers
ASP.NET Core 2.2 and later includes the Microsoft.AspNetCore.Mvc.Api.Analyzers NuGet package containing analyzers for web APIs. The analyzers work with controllers annotated with xref:Microsoft.AspNetCore.Mvc.ApiControllerAttribute, while building on API conventions.
Package installation
Microsoft.AspNetCore.Mvc.Api.Analyzers
can be added with one of the following approaches:
Visual Studio
-
From the Package Manager Console window:
-
Go to View > Other Windows > Package Manager Console.
-
Navigate to the directory in which the ApiConventions.csproj file exists.
-
Execute the following command:
Install-Package Microsoft.AspNetCore.Mvc.Api.Analyzers
-
-
From the Manage NuGet Packages dialog:
- Right-click the project in Solution Explorer > Manage NuGet Packages.
- Set the Package source to "nuget.org".
- Enter "Microsoft.AspNetCore.Mvc.Api.Analyzers" in the search box.
- Select the "Microsoft.AspNetCore.Mvc.Api.Analyzers" package from the Browse tab and click Install.
Visual Studio for Mac
- Right-click the Packages folder in Solution Pad > Add Packages....
- Set the Add Packages window's Source drop-down to "nuget.org".
- Enter "Microsoft.AspNetCore.Mvc.Api.Analyzers" in the search box.
- Select the "Microsoft.AspNetCore.Mvc.Api.Analyzers" package from the results pane and click Add Package.
Visual Studio Code
Run the following command from the Integrated Terminal:
dotnet add ApiConventions.csproj package Microsoft.AspNetCore.Mvc.Api.Analyzers
.NET Core CLI
Run the following command:
dotnet add ApiConventions.csproj package Microsoft.AspNetCore.Mvc.Api.Analyzers
Analyzers for API conventions
OpenAPI documents contain status codes and response types that an action may return. In ASP.NET Core MVC, attributes such as xref:Microsoft.AspNetCore.Mvc.ProducesResponseTypeAttribute and xref:Microsoft.AspNetCore.Mvc.ProducesAttribute are used to document an action. xref:tutorials/web-api-help-pages-using-swagger goes into further detail on documenting your API.
One of the analyzers in the package inspects controllers annotated with xref:Microsoft.AspNetCore.Mvc.ApiControllerAttribute and identifies actions that don't entirely document their responses. Consider the following example:
The preceding action documents the HTTP 200 success return type but doesn't document the HTTP 404 failure status code. The analyzer reports the missing documentation for the HTTP 404 status code as a warning. An option to fix the problem is provided.