diff --git a/ASP.NET Core Basics/src/Contacts/Contacts.csproj b/ASP.NET Core Basics/src/Contacts/Contacts.csproj index daeb03e..be7e935 100644 --- a/ASP.NET Core Basics/src/Contacts/Contacts.csproj +++ b/ASP.NET Core Basics/src/Contacts/Contacts.csproj @@ -19,8 +19,8 @@ - + diff --git a/ASP.NET Core Basics/src/Contacts/Controllers/ContactsApiController.cs b/ASP.NET Core Basics/src/Contacts/Controllers/ContactsApiController.cs index 4fd1e36..585c63c 100644 --- a/ASP.NET Core Basics/src/Contacts/Controllers/ContactsApiController.cs +++ b/ASP.NET Core Basics/src/Contacts/Controllers/ContactsApiController.cs @@ -29,6 +29,9 @@ public IEnumerable GetContact() // GET: api/ContactsApi/5 [HttpGet("{id}")] + [ProducesResponseType(typeof(Contact), 200)] + [ProducesResponseType(typeof(IDictionary), 400)] + [ProducesResponseType(typeof(void), 404)] public async Task GetContact([FromRoute] int id) { if (!ModelState.IsValid) @@ -36,7 +39,7 @@ public async Task GetContact([FromRoute] int id) return BadRequest(ModelState); } - Contact contact = await _context.Contact.SingleOrDefaultAsync(m => m.Id == id); + var contact = await _context.Contact.SingleOrDefaultAsync(m => m.Id == id); if (contact == null) { @@ -48,6 +51,11 @@ public async Task GetContact([FromRoute] int id) // PUT: api/ContactsApi/5 [HttpPut("{id}")] + [ProducesResponseType(typeof(Contact), 200)] + [ProducesResponseType(typeof(IDictionary), 400)] + [ProducesResponseType(typeof(void), 400)] + [ProducesResponseType(typeof(void), 404)] + [ProducesResponseType(typeof(void), 204)] public async Task PutContact([FromRoute] int id, [FromBody] Contact contact) { if (!ModelState.IsValid) @@ -72,10 +80,8 @@ public async Task PutContact([FromRoute] int id, [FromBody] Conta { return NotFound(); } - else - { - throw; - } + + throw; } return NoContent(); @@ -83,6 +89,11 @@ public async Task PutContact([FromRoute] int id, [FromBody] Conta // POST: api/ContactsApi [HttpPost] + [ProducesResponseType(typeof(Contact), 200)] + [ProducesResponseType(typeof(IDictionary), 400)] + [ProducesResponseType(typeof(void), 400)] + [ProducesResponseType(typeof(void), 404)] + [ProducesResponseType(typeof(void), 409)] public async Task PostContact([FromBody] Contact contact) { if (!ModelState.IsValid) @@ -101,10 +112,7 @@ public async Task PostContact([FromBody] Contact contact) { return new StatusCodeResult(StatusCodes.Status409Conflict); } - else - { - throw; - } + throw; } return CreatedAtAction("GetContact", new { id = contact.Id }, contact); @@ -112,6 +120,10 @@ public async Task PostContact([FromBody] Contact contact) // DELETE: api/ContactsApi/5 [HttpDelete("{id}")] + [ProducesResponseType(typeof(Contact), 200)] + [ProducesResponseType(typeof(IDictionary), 400)] + [ProducesResponseType(typeof(void), 400)] + [ProducesResponseType(typeof(void), 404)] public async Task DeleteContact([FromRoute] int id) { if (!ModelState.IsValid) @@ -119,7 +131,7 @@ public async Task DeleteContact([FromRoute] int id) return BadRequest(ModelState); } - Contact contact = await _context.Contact.SingleOrDefaultAsync(m => m.Id == id); + var contact = await _context.Contact.SingleOrDefaultAsync(m => m.Id == id); if (contact == null) { return NotFound(); diff --git a/ASP.NET Core Basics/src/Contacts/Startup.cs b/ASP.NET Core Basics/src/Contacts/Startup.cs index 45c8a50..1ea490d 100644 --- a/ASP.NET Core Basics/src/Contacts/Startup.cs +++ b/ASP.NET Core Basics/src/Contacts/Startup.cs @@ -8,6 +8,7 @@ using Contacts.Models; using Contacts.Services; using Microsoft.AspNetCore.Identity; +using Swashbuckle.AspNetCore.Swagger; namespace Contacts { @@ -52,7 +53,10 @@ public void ConfigureServices(IServiceCollection services) services.AddDbContext(options => options.UseSqlServer(Configuration["Data:ContactsContext:ConnectionString"])); - services.AddSwaggerGen(); + services.AddSwaggerGen(c => + { + c.SwaggerDoc("v1", new Info { Title = "Contacts API", Version = "v1"}); + }); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. @@ -94,7 +98,10 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF }); app.UseSwagger(); - app.UseSwaggerUi(); + app.UseSwaggerUI(c => + { + c.SwaggerEndpoint("/swagger/v1/swagger.json", "Contacts API V1"); + }); } } }