. NET Core Learning Journal II: adding Swagger support

Source: Internet
Author: User

Swagger is a RESTful API document generation tool, when the swagger is added, the Publish API automatically generates swagger pages, records the corresponding interface information, and can test the interface directly on the page.

This article records how the. NET Core WEBAPI Project introduces swagger

1. Create the ASP. Output Type Select Console Application (default)

2. Introducing the swagger package via NuGet, in the Package Manager console input: Install-package swashbuckle.aspnetcore installation.

3. Right-click on the project selection properties to enter the Settings page, select the Build page, the output item to "XML document File" check, save

  

4. Add comments to each of the Valuescontroller interface methods

  

5. Open Startup.cs, add swagger Configuration code

Add the following code in the Configureservices method

Services. Addswaggergen (c =
{
C.swaggerdoc ("v1", new Swashbuckle.AspNetCore.Swagger.Info
{
Title = "Swagger XML API Demo",
Version = "V1",
});

var xmlpath=system.io.path.combine (appcontext.basedirectory, "swagdemo.xml");
C.includexmlcomments (Xmlpath);
});

Add the code in the Configure method

App. Useswagger ();

App. Useswaggerui (c =
{
C.swaggerendpoint ("/swagger/v1/swagger.json", "Swagger CML API Demo v1");
});

Run the project, go to page Http://localhost:55680/swagger (can go to Launchsettings.json file adjustment port and start page), see the specific effect

  

In addition, you can choose a specific method to debug

  

By adding swagger, you can see the API documentation and debug it intuitively.

  

. NET Core Learning Journal II: adding Swagger support

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.