Creating ASP.NET Core 2 Web API in Visual Studio 2017

This tutorial lets us create very basic ASP.NET Core 2 Web API using Visual Studio 2017. We will be creating Contacts API which let’s do popular CRUD operations. ASP.NET Web API is a framework that makes it easy to build HTTP services that reach a broad range of clients, including browsers and mobile devices.

ASP.NET Web API is an ideal platform for building RESTful applications on the .NET Framework.

What’s in this tutorial?

  • Contacts API Overview
  • Create ASP.NET Core 2 Web API project
  • Create Contacts model
  • Create and Register repository class for Contacts
  • Add Contacts API Controller
  • Writing Contacts CRUD methods
  • Testing WEB API using POSTMAN

Step 1: Contacts API Overview

The Contacts API is very simple, basic Web API which does CRUD operations. I have focused on writing web API rather than integrating it with databases.

Step 2: Create ASP.NET Core 2 Web API project

Install .NET Core 2.0 SDK and Visual Studio 2017 (15.3)

Open Visual Studio 2017, create “New Project” with name “ContactsApi“ From ASP.NET Core templates select “Web API” for ASP.NET Core 2.0 (I haven’t selected any Authentication, we will add them later)

Program.cs file is an entry point when application run, that’s right public static void main(). ASP.NET Core apps are considered as console apps.

Step 3: Creating Contacts model

Contacts class is the centre of this Web API project. Its POCO class containing some properties which are self-explanatory. Right click “ContactsApi“ solution, create folder “Models“; under this “Models“ folder create C# class “Contacts.cs“ and copy this code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
using System;
namespace ContactsApi.Models
{
public class Contacts
{
public string FirstName { get; set; }
public string LastName { get; set; }
public bool IsFamilyMember { get; set; }
public string Company { get; set; }
public string JobTitle { get; set; }
public string Email { get; set; }
public string MobilePhone { get; set; }
public DateTime DateOfBirth { get; set; }
public DateTime AnniversaryDate { get; set; }
}
}

Step 4: Create and Register repository class for Contacts

The use of repository classes is really optional, but I have added it so that we can connect to any databases later. Create “Repository“ folder under “ContactsApi“ solution, we will add one C# interface file and C# class file implementing this interface. Create “IContactsRepository.cs“ interface file in “Repository“ folder and copy below code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
using ContactsApi.Models;
using System.Collections.Generic;

namespace ContactsApi.Repository
{
public interface IContactsRepository
{
void Add(Contacts item);
IEnumerable<Contacts> GetAll();
Contacts Find(string key);
void Remove(string Id);
void Update(Contacts item);
}
}

Create “ContactsRepository.cs“ class file, implement “IContactsRepository“ and copy below code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using System.Collections.Generic;
using System.Linq;
using ContactsApi.Models;

namespace ContactsApi.Repository
{
public class ContactsRepository : IContactsRepository
{
static List<Contacts> ContactsList = new List<Contacts>();

public void Add(Contacts item)
{
ContactsList.Add(item);
}

public Contacts Find(string key)
{
return ContactsList
.Where(e => e.MobilePhone.Equals(key))
.SingleOrDefault();
}

public IEnumerable<Contacts> GetAll()
{
return ContactsList;
}

public void Remove(string Id)
{
var itemToRemove = ContactsList.SingleOrDefault(r => r.MobilePhone == Id);
if (itemToRemove != null)
ContactsList.Remove(itemToRemove);
}

public void Update(Contacts item)
{
var itemToUpdate = ContactsList.SingleOrDefault(r => r.MobilePhone == item.MobilePhone);
if (itemToUpdate != null)
{
itemToUpdate.FirstName = item.FirstName;
itemToUpdate.LastName = item.LastName;
itemToUpdate.IsFamilyMember = item.IsFamilyMember;
itemToUpdate.Company = item.Company;
itemToUpdate.JobTitle = item.JobTitle;
itemToUpdate.Email = item.Email;
itemToUpdate.MobilePhone = item.MobilePhone;
itemToUpdate.DateOfBirth = item.DateOfBirth;
itemToUpdate.AnniversaryDate = item.AnniversaryDate;
}
}
}
}

ASP.NET Core 2 provides out of box support for Dependency Injection, we will include that in our “ConfigureServices“ method of Startup.cs. We will see entire code in Step 5

Step 5: Add Contacts API Controller

It’s time to add the controller API which acts as Web API. Create “Controllers“ folder under “ContactsApi“ project solution and add C# class file “ContactsController.cs“; copy below code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using ContactsApi.Models;
using ContactsApi.Repository;
using Microsoft.AspNetCore.Mvc;

using System.Collections.Generic;

namespace ContactsApi.Controllers
{
[Route("api/[controller]")]
public class ContactsController : Controller
{
public IContactsRepository ContactsRepo { get; set; }

public ContactsController(IContactsRepository _repo)
{
ContactsRepo = _repo;
}

[HttpGet]
public IEnumerable<Contacts> GetAll()
{
return ContactsRepo.GetAll();
}

[HttpGet("{id}", Name = "GetContacts")]
public IActionResult GetById(string id)
{
var item = ContactsRepo.Find(id);
if (item == null)
{
return NotFound();
}
return new ObjectResult(item);
}

[HttpPost]
public IActionResult Create(\[FromBody\] Contacts item)
{
if (item == null)
{
return BadRequest();
}
ContactsRepo.Add(item);
return CreatedAtRoute("GetContacts", new { Controller = "Contacts", id = item.MobilePhone }, item);
}

[HttpPut("{id}")]
public IActionResult Update(string id, \[FromBody\] Contacts item)
{
if (item == null)
{
return BadRequest();
}
var contactObj = ContactsRepo.Find(id);
if (contactObj == null)
{
return NotFound();
}
ContactsRepo.Update(item);
return new NoContentResult();
}

[HttpDelete("{id}")]
public void Delete(string id)
{
ContactsRepo.Remove(id);
}
}
}

Some quick notes of this ContactsController

  1. [Route(“api/[controller]”)] - this used attribute based routing to access the ASP.NET Core Web API.
  2. ContactsRepo is instantiated using dependency injection which we configure in services.cs.
  3. GetAll() is simple HttpGet method which gets all contacts
  4. GetById fetches contact based on the mobile phone. Its given HttpGet with Name attribute so that we can use that in Create method to be used for location header.
  5. Create method after inserting contact, returns 201 response and provides location header.

Note: HTTP Status codes are now written as BadReqest(), NotFound(), Unauthorized() etc

Step 6: Testing Contacts Web API using POSTMAN

ASP.NET Core 2 Web API allows disabling of launching browser when we debug the application. Right click on “ContactsApi“, goto “Properties“ and Select Debug.
You can uncheck “Launch URL“ check box to ensure it doesn’t the open browser (this is optional). RUN/ DEBUG application by clicking “IIS Express” which starts Web API, we can use other ways to start the application.

Since Web API does CRUD operations on Contacts using in memory collections.
We will start with POST, GET, PUT, DELETE operations Using Chrome’s POSTMAN extension to test client, it’s very easy to use. Even Fiddler can also be used for testing.

Contacts API’s POST operation

Contacts API POST operation

Point 2 provides location header which can be used fetch result.

Contacts API GET operation

Contacts Api GET operation

Contacts API PUT operation

Contacts Api PUT operation

Contacts API DELETE operation

Contacts Api DELETE operation

This sample will be made better by adding logging, connecting to the database using EF Core or EF 6 or any ORMs.

Read Integrating EF Core with ASP.NET Core 2 Web API

The source code of this article is on mithunvp github repo