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