What is a REST API?
Stateless API
Stateful API
What is Swagger?
"Swagger is a powerful yet easy-to-use suite of API developer tools for teams and individuals, enabling development across the entire API lifecycle, from design and documentation, to test and deployment.
Swagger consists of a mix of open source, free and commercially available tools that allow anyone, from technical engineers to street smart product managers to build amazing APIs that everyone loves."
Benefits of using Swagger
- Easily tested APIs, being able to simulate the usage of any method;
- A complete view of your API methods and controllers, Swagger groups the API methods per each controller;
- API documentation, Swagger can be used as part of the documentation.
- Much more benefits can be found here.
Implementation Step by Step





- public class Startup
- {
- public Startup( IConfiguration configuration )
- {
- Configuration = configuration;
- }
- public IConfiguration Configuration { get; }
- // This method gets called by the runtime. Use this method to add services to the container.
- public void ConfigureServices( IServiceCollection services )
- {
- services.AddMvc().SetCompatibilityVersion( CompatibilityVersion.Version_2_1 );
- // Register Swagger
- services.AddSwaggerGen( c =>
- {
- c.SwaggerDoc( "v1", new Info { Title = "Sample API", Version = "version 1" } );
- } );
- }
- // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
- public void Configure( IApplicationBuilder app, IHostingEnvironment env )
- {
- if ( env.IsDevelopment() )
- {
- app.UseDeveloperExceptionPage();
- // Enable middleware to serve generated Swagger as a JSON endpoint.
- app.UseSwagger();
- // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
- // specifying the Swagger JSON endpoint.
- app.UseSwaggerUI( c =>
- {
- c.SwaggerEndpoint( "/swagger/v1/swagger.json", "My API V1" );
- } );
- }
- app.UseMvc();
- }
- }
Project customization

- public static class ValueSamples
- {
- public static Dictionary<int, string> MyValue;
- public static void Initialize()
- {
- MyValue = new Dictionary<int, string>();
- MyValue.Add( 0, "Value 0" );
- MyValue.Add( 1, "Value 1" );
- MyValue.Add( 2, "Value 2" );
- }
- }
- [Route( "api/[controller]" )]
- [ApiController]
- public class ValuesController : ControllerBase
- {
- public ValuesController()
- {
- ValueSamples.Initialize();
- }
- // GET api/values
- [HttpGet]
- public ActionResult<Dictionary<int, string>> Get()
- {
- return ValueSamples.MyValue;
- }
- // GET api/values/5
- [HttpGet( "{id}" )]
- public ActionResult<string> Get( int id )
- {
- return ValueSamples.MyValue.GetValueOrDefault( id );
- }
- // POST api/values
- [HttpPost]
- public void Post( [FromBody] string value )
- {
- var maxKey = ValueSamples.MyValue.Max( x => x.Key );
- ValueSamples.MyValue.Add( maxKey + 1, value );
- }
- // PUT api/values/5
- [HttpPut( "{id}" )]
- public void Put( int id, [FromBody] string value )
- {
- ValueSamples.MyValue.Add( id, value );
- }
- // DELETE api/values/5
- [HttpDelete( "{id}" )]
- public void Delete( int id )
- {
- ValueSamples.MyValue.Remove( id );
- }
- }
Using the Swagger

Testing the "Get all" method

Testing getting a single result method

Testing to get a single non-existent record

Swagger Response

Testing the post method

Swagger Response

- https://swagger.io/
- https://www.nuget.org/packages/Swashbuckle.AspNetCore
- https://docs.microsoft.com/en-us/aspnet/core/tutorials/getting-started-with-swashbuckle

Nakul ChaudhariPosted Oct 14, 2019, 7:05 AM
Thanks for the article. I am successfully integrated Swagger but facing issue while consuming it in web application. I am using below code but getting 404 error "Using (var httpClient = new HttpClient()) { using (var response = await httpClient.GetAsync("my end point")) { string apiResponse = await response.Content.ReadAsStringAsync(); } }" Can you please help me on this?