Adding Swagger to a .Net core application

If you have used Swagger with a website to document your API, then you know it has some nice features for testing your API as well as viewing the structure and documentation.

This is brief walkthrough of setting up Swagger.

First install the Swashbuckle nuget package.

In the startup.cs file of your .Net core web app, change ConfigureServices to have the following.

public IServiceProvider ConfigureServices(IServiceCollection services)

        {

            // Add framework services.

            services.AddMvc()

                .AddControllersAsServices();

            services.AddSwaggerGen(c =>

            {

                c.SwaggerDoc(“v1”, new Info { Title = “MyAPI”, Version = “v1” });

            });

            services.ConfigureSwaggerGen(c =>

            {

                c.IncludeXmlComments(GetXmlCommentsPath(PlatformServices.Default.Application));

            });

           

            return ConfigureIoC(services);

        }


And add this:

private string GetXmlCommentsPath(ApplicationEnvironment appEnvironment)



        {



            return Path.Combine(appEnvironment.ApplicationBasePath, "SolutionName.Project.ProjectAPI.xml");

//The above is coming from the .Net core project properties, build tab. // You need to check the box to create the XML documentation and use the file name from there. 

        }

ProjectProperties

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s