ASP. NET CORE 2.0 Swagger

Source: Internet
Author: User

Package

Adding and configuring swagger middleware

to add the swagger generator to a service collection Startup.ConfigureServices in a method :

         Public voidconfigureservices (iservicecollection services) {services.            Addmvc (); Services. Addswaggergen (c={C.swaggerdoc ("v1",NewInfo {Title ="Woorldcup Api", Version ="v1" }); //Set The comments path for the Swagger JSON and UI.                varXMLFile = $"{assembly.getexecutingassembly (). GetName (). Name}.xml"; varXmlpath =Path.Combine (Appcontext.basedirectory, xmlfile);            C.includexmlcomments (Xmlpath);        }); }

In the Startup.configure method, enable the middleware that serves the generated JSON document and the Swagger UI:

         Public void Configure (Iapplicationbuilder app, ihostingenvironment env)        {            if  (env. Isdevelopment ())            {                app. Usedeveloperexceptionpage ();            }            App. Useswagger ();             = =            {                c.swaggerendpoint ("/swagger/v1/swagger.json" " Woorldcup Api V1 " );            });            App. Usemvc ();        }

Controller tag

usingMicrosoft.AspNetCore.Hosting;usingMICROSOFT.ASPNETCORE.MVC;usingSystem.ComponentModel;usingSystem.ComponentModel.DataAnnotations;usingSystem.Linq;usingWorldcup.data;namespaceworldcup.controllers{/// <summary>    ///Game related/// </summary>[Route ("Api/[controller]")]     Public classMatchcontroller:controller {Private ReadOnlyWorldcupdbcontext _worldcupdb; Private ReadOnlyihostingenvironment _hostingenvironment;  PublicMatchcontroller (Worldcupdbcontext worldcupdb) {_worldcupdb=worldcupdb; }        /// <summary>        ///Get all teams/// </summary>        /// <returns></returns>[HttpGet ("Getallteam")]         Publiciactionresult Getallteam (TodoItem item) {returnOk (New{Result=_worldcupdb.worldcup_country.tolist ()}); }    }     Public classTodoItem { Public LongId {Get;Set; } [Required] Public stringName {Get;Set; } [DefaultValue (false)]         Public BOOLIscomplete {Get;Set; } }}

Effect

ASP. NET CORE 2.0 Swagger

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.