Creating ASP.NET Core MVC 6 Web API using Visual Studio 2015

This tutorial lets us create very basic ASP.NET MVC 6 Web API using Visual Studio 2015. We will be creating Contacts APIwhich lets 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 Web API project
  • Create Contacts model
  • Create and Register repository class for Contacts
  • Add Contacts API Controller
  • Enable CamelCasePropertyNamesContractResolver
  • 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. This table summaries Contacts API which we’ll create

GET /api/contacts

GET /api/contacts/{id}

POST /api/contacts

PUT /api/contacts/{id}

DELETE /api/contacts/{id}

Step 2: Create ASP.NET Core Web API project

Install ASP.NET Core RC2 tools is must

Open Visual Studio 2015 Update 2, create “New Project” with name “ContactsApi” targeting .NET Framework 4.6. We can target .NET 4.5.1 or .NET 4.5.2

From ASP.NET Core templates select “Web API” as shown in image (I haven’t selected any Authentication, we will add them later)

Creating ASP.NET Core Web API

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

Step 3: Packages included for ASP.NET Core MVC 6 Web API

The packages included are “MVC”, “EnvironmentalVariables”, “JSON”, “Logging”. (This is generated by default, do not copy this).

Step 4: Creating Contacts model

Contacts class is centre of this MVC 6 Web API. 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

Step 5: 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

Create “ContactsRepository.cs” class file, implement “IContactsRepository” and copy below code

ASP.NET MVC 6 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 7

Step 6: Add Contacts API Controller

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

Some quick notes of this ContactsController

  1. [Route(“api/[controller]”)] — this used attribute based routing to access the MVC 6 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 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 7: Enable CamelCasePropertyNamesContractResolver

Any Web Api (REST based) should return JSON response in form of Camel Case so that we can sure consume the API in any client. We need to enable CamelCasePropertyNamesContractResolver in Configure Services.

Here is the Startup.cs file which has all code needed for running this Contacts MVC 6 Web API.

Hope that this article help you to get start in Web API and MVC format.

See ya! ;)