Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ASP.NET Core Basics/src/Contacts/Contacts.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
<PackageReference Include="Swashbuckle" Version="6.0.0-beta902" />
<PackageReference Include="BundlerMinifier.Core" Version="2.4.337" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="1.0.0" />
</ItemGroup>

<Target Name="PrepublishScript" BeforeTargets="PrepareForPublish">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,17 @@ public IEnumerable<Contact> GetContact()

// GET: api/ContactsApi/5
[HttpGet("{id}")]
[ProducesResponseType(typeof(Contact), 200)]
[ProducesResponseType(typeof(IDictionary<string, string>), 400)]
[ProducesResponseType(typeof(void), 404)]
public async Task<IActionResult> GetContact([FromRoute] int id)
{
if (!ModelState.IsValid)
{
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)
{
Expand All @@ -48,6 +51,11 @@ public async Task<IActionResult> GetContact([FromRoute] int id)

// PUT: api/ContactsApi/5
[HttpPut("{id}")]
[ProducesResponseType(typeof(Contact), 200)]
[ProducesResponseType(typeof(IDictionary<string, string>), 400)]
[ProducesResponseType(typeof(void), 400)]
[ProducesResponseType(typeof(void), 404)]
[ProducesResponseType(typeof(void), 204)]
public async Task<IActionResult> PutContact([FromRoute] int id, [FromBody] Contact contact)
{
if (!ModelState.IsValid)
Expand All @@ -72,17 +80,20 @@ public async Task<IActionResult> PutContact([FromRoute] int id, [FromBody] Conta
{
return NotFound();
}
else
{
throw;
}

throw;
}

return NoContent();
}

// POST: api/ContactsApi
[HttpPost]
[ProducesResponseType(typeof(Contact), 200)]
[ProducesResponseType(typeof(IDictionary<string, string>), 400)]
[ProducesResponseType(typeof(void), 400)]
[ProducesResponseType(typeof(void), 404)]
[ProducesResponseType(typeof(void), 409)]
public async Task<IActionResult> PostContact([FromBody] Contact contact)
{
if (!ModelState.IsValid)
Expand All @@ -101,25 +112,26 @@ public async Task<IActionResult> PostContact([FromBody] Contact contact)
{
return new StatusCodeResult(StatusCodes.Status409Conflict);
}
else
{
throw;
}
throw;
}

return CreatedAtAction("GetContact", new { id = contact.Id }, contact);
}

// DELETE: api/ContactsApi/5
[HttpDelete("{id}")]
[ProducesResponseType(typeof(Contact), 200)]
[ProducesResponseType(typeof(IDictionary<string, string>), 400)]
[ProducesResponseType(typeof(void), 400)]
[ProducesResponseType(typeof(void), 404)]
public async Task<IActionResult> DeleteContact([FromRoute] int id)
{
if (!ModelState.IsValid)
{
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();
Expand Down
11 changes: 9 additions & 2 deletions ASP.NET Core Basics/src/Contacts/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Contacts.Models;
using Contacts.Services;
using Microsoft.AspNetCore.Identity;
using Swashbuckle.AspNetCore.Swagger;

namespace Contacts
{
Expand Down Expand Up @@ -52,7 +53,10 @@ public void ConfigureServices(IServiceCollection services)
services.AddDbContext<ContactsContext>(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.
Expand Down Expand Up @@ -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");
});
}
}
}