In this tutorial, I'll show you how to use Swagger to test your Web API in ASP.NET MVC. Swagger is a library that helps you intergrate into ASP.NET to test Web API. You can download Swagger from Nuget Package Manager or you can download it directly from https://github.com/heldersepu/Swagger-Net
We will use Entity Framework Database First to practice our demo, if you don't already know about it. I think you should read Getting Started with Entity Framework Database First
To practice demo, you should create a new ASP.NET MVC project, remember to check the include Web API
When the asp.net mvc project has finished creating, we will create an API Controller by right-clicking on Controllers folder->Add->Controller
Select API 2 Controller with actions, using Entity Framework->Add
Enter your web api controller name->Add
ASP.NET MVC Razor automatically generates the code as shown below
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Description;
using WebAppDemo;
namespace WebAppDemo.Controllers
{
public class CustomerController : ApiController
{
private NorthwindEntities db = new NorthwindEntities();
// GET: api/Customer
public IQueryable<Customer> GetCustomers()
{
return db.Customers;
}
// GET: api/Customer/5
[ResponseType(typeof(Customer))]
public async Task<IHttpActionResult> GetCustomer(string id)
{
Customer customer = await db.Customers.FindAsync(id);
if (customer == null)
{
return NotFound();
}
return Ok(customer);
}
// PUT: api/Customer/5
[ResponseType(typeof(void))]
public async Task<IHttpActionResult> PutCustomer(string id, Customer customer)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (id != customer.CustomerID)
{
return BadRequest();
}
db.Entry(customer).State = EntityState.Modified;
try
{
await db.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!CustomerExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return StatusCode(HttpStatusCode.NoContent);
}
// POST: api/Customer
[ResponseType(typeof(Customer))]
public async Task<IHttpActionResult> PostCustomer(Customer customer)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
db.Customers.Add(customer);
try
{
await db.SaveChangesAsync();
}
catch (DbUpdateException)
{
if (CustomerExists(customer.CustomerID))
{
return Conflict();
}
else
{
throw;
}
}
return CreatedAtRoute("DefaultApi", new { id = customer.CustomerID }, customer);
}
// DELETE: api/Customer/5
[ResponseType(typeof(Customer))]
public async Task<IHttpActionResult> DeleteCustomer(string id)
{
Customer customer = await db.Customers.FindAsync(id);
if (customer == null)
{
return NotFound();
}
db.Customers.Remove(customer);
await db.SaveChangesAsync();
return Ok(customer);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
private bool CustomerExists(string id)
{
return db.Customers.Count(e => e.CustomerID == id) > 0;
}
}
}
Run your project, then change your url to http://localhost:49814/swagger/ui/index
Remember change your port http://localhost:[your port]/swagger/ui/index
Click Get to test your web api
Click Excute to run get method
You can see your data show as above