Installation and use of the ASP. NET Core Api using the Swagger Management documentation tutorial

Source: Internet
Author: User

this week because the company needs me to do an API program, this week for three days I have been in the core API and framework API between the tangled. Do not know which one to use to do the project, think about the decision. Since both of me didn't use the core API to do it. is also a kind of exercise for oneself!

OK, let's go back to the chase!

The swagger under the core and the swagger under the traditional framework MVC are not the same!

The difference between the two:

One : The referenced assemblies are not the same.

Second: There are different places to configure after installing the assembly,

after installation of the swagger under the framework, there will be swaggerconfig and swaggernet This even a file. It is also configured under these two files.

After the installation of the core does not have these two files, it is configured in the core project startup file Configuration!

To start installing swagger:

Find the NuGet Management Pack for your API project, and then search for "Swashbuckle.aspnetcore". Find the package in the picture below and install it!

     

after the installation is complete, it will be displayed in your package! (as shown)

The next step is the configuration problem, which is configured in the Startup.cs file.

First, the following namespaces are introduced:

using Swashbuckle.AspNetCore.Swagger;

Add the Swagger generator to the service collection in the Startup.configureservices method: (This is a short version, we will add more specific content in the future)

// registers the swagger generator, defining one and more swagger document services. Addswaggergen (c +=     {c.swaggerdoc ("ehhd"new" EHHD " " Interface Documentation "  });});

In the Startup.Configure method, enable middleware to serve the generated JSON document and Swagger UI:

  // configuration swagger  must be added to the app. Usemvc the front             app. Useswagger ();             // Swagger core needs to be configured to  be added to the app. Usemvc the front            app. Useswaggerui (c + =            {                c.swaggerendpoint ("/swagger/ehhd/swagger.json " " Ehhdapi " );            });

This launches the project by adding swagger to the website's localhost port number to browse the API documentation. However, such documents do not have comments. Adding comments requires a bit of action!

To enable XML annotations:
    • Right-click the project in Solution Explorer and select Properties
    • Look at the XML document file box under the output section of the Build tab ( note the XML file name in the red box tag, and you'll need it later!). )

Attention:

? 1. File names and paths are case-sensitive for Linux or non-Windows operating systems. For example, the "swaggerdemo.xml" file is valid on Windows but is not valid on CentOS.

? 2. Get the application path, recommended in Path.GetDirectoryName(typeof(Program).Assembly.Location) this way or • Appcontext.basedirectory this to get

3. The code that needs to be added to the comment is in the comments in the following code, which is the bottom three code. The XML file name of the second line takes care of the XML file name for your own project!

  //configuration items required by the swaggerServices. Addswaggergen (c =            {                //add swagger.C.swaggerdoc ("EHHD",NewInfo {Version="EHHD", Title="Physician-Patient Interaction Interface Documentation", Description="This EHHD Api",                    //Terms of serviceTermsofservice ="None",                    //Author InformationContact =NewContact {Name="sir_ Bo", Email=string. Empty, Url="http://www.cnblogs.com/Scholars/"                    },                    //LicenseLicense =NewLicense {Name="License Name", the Url="http://www.cnblogs.com/Scholars/"                    }                }); //The following three methods set the XML document comment path for the Swagger JSON and UI                varBasePath = Path.getdirectoryname (typeof(program). Assembly.location);//get the directory where the application is located (absolutely, not affected by the working directory, we recommend this method to get the path)                varXmlpath = Path.Combine (BasePath,"Ehhd_api.xml");            C.includexmlcomments (Xmlpath);            }); //code required to read the database connection statement configured in the Aoosettings.jsonServices. Configure<dbcontext> (Configuration.getsection ("sqlconfiguration")); }

So the entire Swagger API document has been built, let's look at the effect!

However, one problem is that each time you run a project, you Xu Xian yourself to manually enter the path after the port number of the page swagger. It's very upsetting, so let's configure it to automatically fetch the Swagger API page!

Expand the project's properties drop-down and open Launchsettings.json. You can change the place of the arrow mark in the swagger.

Installation and use of the ASP. NET Core Api using the Swagger Management documentation tutorial

Related Article

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.