. NET core configuration swagger encounters with pits

Source: Internet
Author: User

Swagger can be one of the most popular rest APIs documentation generation tools for the following reasons:

    • Swagger can generate an interactive API console that developers can use to quickly learn and experiment with the API.
    • Swagger can generate client SDK code for implementations on a variety of different platforms.
    • Swagger files can be automatically generated from code annotations on many different platforms.
    • Swagger has a strong community with many powerful contributors.

Anyway is very 6, very good, my usual interface development will use swagger, feel very convenient, not how to write documents can quickly and the front end of the docking (I in the actual development and the time of the joint will be used to postman and fiddler and other interface debugging tools, are very useful, recommended here , I will not explain the usage.)

But today, in the. NET core environment, the first configuration of swagger encountered a small pit, the configuration of the Internet has a lot of information,

can refer to https://docs.microsoft.com/en-us/aspnet/core/tutorials/web-api-help-pages-using-swagger?view=aspnetcore-2.1

The first step,

Nuget packages installation, using the Package Manager console, install the command:Install-Package Swashbuckle.AspNetCore -Pre

Step Two,

To add a configuration to the startup file:

 Public voidconfigureservices (iservicecollection services) {//ADD Framework Services.Services. Addmvc (). Addjsonoptions (Options=options. Serializersettings.contractresolver=NewNewtonsoft.Json.Serialization.DefaultContractResolver ());//JSON first letter lowercase resolutionServices. Addswaggergen (Options={options. Swaggerdoc ("v1",NewInfo {Version="v1", Title="Mssystem API"        }); //determine base path for the application.         varBasePath =PlatformServices.Default.Application.ApplicationBasePath; //Set The comments path for the swagger JSON and UI.         varXmlpath = Path.Combine (BasePath,"MsSystem.API.xml"); Options.    Includexmlcomments (Xmlpath); });}

 Public voidConfigure (Iapplicationbuilder app, Ihostingenvironment env, iloggerfactory loggerfactory) { Loggerfactory.addconsole (Configuration.getsection ("Logging"));    Loggerfactory.adddebug (); if(env. Isdevelopment ()) {app.    Usedeveloperexceptionpage (); }    Else{app. Useexceptionhandler ("/home/error"); }    //app. Usestaticfiles ();app.    Usemvc (); //app. USEMVC (routes =//{    //routes. MapRoute (//Name: "Default",//Template: "Api/{controller}/{action}",//defaults:new {controller = "Home", action = "Index"}); //});app.    Useswagger (); App. Useswaggerui (c={c.swaggerendpoint ("/swagger/v1/swagger.json","Mssystem API V1"); });}

Then direct access to address http://localhost: Port number/swagger/, but found an error

Here we also need to configure the generated output directory

Right-click Project properties to generate, see the following interface, check the XML document file, and then press as input configuration, here MsSystem.API.xml is the name of the above code

Then it's done, F5 run.

. NET core configuration swagger encounters with pits

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.