Skip to content

Commit 64c998a

Browse files
authored
Update Contacts project to use a version of Swashbuckle that works with ASP.NET Core 2 (#17)
1 parent 4eef768 commit 64c998a

File tree

3 files changed

+32
-13
lines changed

3 files changed

+32
-13
lines changed

ASP.NET Core Basics/src/Contacts/Contacts.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919

2020
<ItemGroup>
2121
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
22-
<PackageReference Include="Swashbuckle" Version="6.0.0-beta902" />
2322
<PackageReference Include="BundlerMinifier.Core" Version="2.4.337" />
23+
<PackageReference Include="Swashbuckle.AspNetCore" Version="1.0.0" />
2424
</ItemGroup>
2525

2626
<Target Name="PrepublishScript" BeforeTargets="PrepareForPublish">

ASP.NET Core Basics/src/Contacts/Controllers/ContactsApiController.cs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,17 @@ public IEnumerable<Contact> GetContact()
2929

3030
// GET: api/ContactsApi/5
3131
[HttpGet("{id}")]
32+
[ProducesResponseType(typeof(Contact), 200)]
33+
[ProducesResponseType(typeof(IDictionary<string, string>), 400)]
34+
[ProducesResponseType(typeof(void), 404)]
3235
public async Task<IActionResult> GetContact([FromRoute] int id)
3336
{
3437
if (!ModelState.IsValid)
3538
{
3639
return BadRequest(ModelState);
3740
}
3841

39-
Contact contact = await _context.Contact.SingleOrDefaultAsync(m => m.Id == id);
42+
var contact = await _context.Contact.SingleOrDefaultAsync(m => m.Id == id);
4043

4144
if (contact == null)
4245
{
@@ -48,6 +51,11 @@ public async Task<IActionResult> GetContact([FromRoute] int id)
4851

4952
// PUT: api/ContactsApi/5
5053
[HttpPut("{id}")]
54+
[ProducesResponseType(typeof(Contact), 200)]
55+
[ProducesResponseType(typeof(IDictionary<string, string>), 400)]
56+
[ProducesResponseType(typeof(void), 400)]
57+
[ProducesResponseType(typeof(void), 404)]
58+
[ProducesResponseType(typeof(void), 204)]
5159
public async Task<IActionResult> PutContact([FromRoute] int id, [FromBody] Contact contact)
5260
{
5361
if (!ModelState.IsValid)
@@ -72,17 +80,20 @@ public async Task<IActionResult> PutContact([FromRoute] int id, [FromBody] Conta
7280
{
7381
return NotFound();
7482
}
75-
else
76-
{
77-
throw;
78-
}
83+
84+
throw;
7985
}
8086

8187
return NoContent();
8288
}
8389

8490
// POST: api/ContactsApi
8591
[HttpPost]
92+
[ProducesResponseType(typeof(Contact), 200)]
93+
[ProducesResponseType(typeof(IDictionary<string, string>), 400)]
94+
[ProducesResponseType(typeof(void), 400)]
95+
[ProducesResponseType(typeof(void), 404)]
96+
[ProducesResponseType(typeof(void), 409)]
8697
public async Task<IActionResult> PostContact([FromBody] Contact contact)
8798
{
8899
if (!ModelState.IsValid)
@@ -101,25 +112,26 @@ public async Task<IActionResult> PostContact([FromBody] Contact contact)
101112
{
102113
return new StatusCodeResult(StatusCodes.Status409Conflict);
103114
}
104-
else
105-
{
106-
throw;
107-
}
115+
throw;
108116
}
109117

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

113121
// DELETE: api/ContactsApi/5
114122
[HttpDelete("{id}")]
123+
[ProducesResponseType(typeof(Contact), 200)]
124+
[ProducesResponseType(typeof(IDictionary<string, string>), 400)]
125+
[ProducesResponseType(typeof(void), 400)]
126+
[ProducesResponseType(typeof(void), 404)]
115127
public async Task<IActionResult> DeleteContact([FromRoute] int id)
116128
{
117129
if (!ModelState.IsValid)
118130
{
119131
return BadRequest(ModelState);
120132
}
121133

122-
Contact contact = await _context.Contact.SingleOrDefaultAsync(m => m.Id == id);
134+
var contact = await _context.Contact.SingleOrDefaultAsync(m => m.Id == id);
123135
if (contact == null)
124136
{
125137
return NotFound();

ASP.NET Core Basics/src/Contacts/Startup.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Contacts.Models;
99
using Contacts.Services;
1010
using Microsoft.AspNetCore.Identity;
11+
using Swashbuckle.AspNetCore.Swagger;
1112

1213
namespace Contacts
1314
{
@@ -52,7 +53,10 @@ public void ConfigureServices(IServiceCollection services)
5253
services.AddDbContext<ContactsContext>(options =>
5354
options.UseSqlServer(Configuration["Data:ContactsContext:ConnectionString"]));
5455

55-
services.AddSwaggerGen();
56+
services.AddSwaggerGen(c =>
57+
{
58+
c.SwaggerDoc("v1", new Info { Title = "Contacts API", Version = "v1"});
59+
});
5660
}
5761

5862
// 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
9498
});
9599

96100
app.UseSwagger();
97-
app.UseSwaggerUi();
101+
app.UseSwaggerUI(c =>
102+
{
103+
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Contacts API V1");
104+
});
98105
}
99106
}
100107
}

0 commit comments

Comments
 (0)