Creating a .Net Core 3 web API with Swagger. This example in complete inside the api folder.
Download the Visual Studio 2019 here and install it.
Create a new project:
Choose ASP.Net Core Web Application:
Name the project and configure the local path:
Select an empty .Net Core project and uncheck the HTTPS and Docker support options:
After the solution be compiled, create a folder named "Controllers" and add into it a class named "TestController" with an async method:
Now, let's config the "Startup" class:
Run the project in debug mode and access the URL below:
http://localhost:<API-PORT>/api/Test
If the "OK!" message be displayed in the browser, our web API is running!
Inside the Visual Studio 2019, open the Package Manager Console and run the following commands to install the Swashbuckle version compatible with .Net Core 3:
Install-Package Swashbuckle.AspNetCore -Version 5.0.0-rc3
Install-Package Swashbuckle.AspNetCore.Filters -Version 5.0.0-rc3
Install-Package Swashbuckle.AspNetCore.Annotations -Version 5.0.0-rc3
Open the "Startup" class again and modify the "ConfigureServices" method:
And the "Configure" method as well:
Open the project's properties window, go to "Build", check the "XML documentation file" option and type your project name with the XML extension:
Go to "Debug" and type "swagger" inside the input field above the environment variables list:
WARNING: To avoid the MSB3073 error during the compilation, edit the project's .csproj file by adding the tag OpenApiGenerateDocuments inside PropertyGroup: |
---|
<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<OpenApiGenerateDocuments>false</OpenApiGenerateDocuments>
</PropertyGroup>
Inside the "TestController", put a simple annotation above the "Get" method:
Now, let's run the project again. The Swagger page will open in this URL:
http://localhost:<API-PORT>/swagger/index.html
This will be the result:
Try the "Get" method and the "OK!" message will return from the API.