Tuesday, June 14, 2016

How to create Rest API(Web API) with ASP.NET Core 1.0

Recently Microsoft has released ASP.NET Core 1.0 RC2, I am getting lots of request from readers that about creating Web API. So I thought it will be a good idea to write a blog post about how to create Rest API(Web API) with ASP.NET Core 1.0.

So let’s get started via creating an ASP.NET Core 1.0 Web Application like following.


Once you click on ASP.NET Web Application, It will ask whether you need to create Web Application or Web API application. We are going to Web API so I am going to select Web API Application like following. Please note that in ASP.NET Core 1.0 there is no separate libraries or DLLs required for creating web APIs. This is just a project template.


Now once you click on OK It will create a Web API application with default values controller and program.cs.  As you know Program.cs is now starting point for the ASP.NET Core 1.0 application so It contains all the required configuration and startup items. Following is a code for that.
using System.IO;
using Microsoft.AspNetCore.Hosting;

namespace CoreWebApi
    public class Program
        public static void Main(string[] args)
            var host = new WebHostBuilder()


Here you can see that there is a WebHostBuilder class which hosts the application and there is some configuration for using this application on IIS and Kestrel which is  a cross-platform server from Microsoft.

Now let’s create our model class first. I have created an Employee model class like following.
namespace CoreWebApi.Model
    public class Employee
        public int EmployeeId { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Designation { get; set; }
Since now our model is ready, It’s time to create the controller. You can create web API controller via add new item like following.


And here the code for our get Method.
using System.Collections.Generic;
using CoreWebApi.Model;
using Microsoft.AspNetCore.Mvc;

namespace CoreWebApi.Controllers
    public class EmployeeController : Controller
        // GET: api/values
        public IEnumerable<Employee> Get()
           var employees = new List<Employee>
               new Employee {EmployeeId = 1,FirstName = "Jalpesh",LastName = "Vadgama",Designation = "Technical Architect"},
               new Employee {EmployeeId = 2,FirstName = "Vishal",LastName = "Vadgama",Designation = "Technical Lead"}
            return employees;

Here you can see that I have created a get method that returns a List of Employee. Now Let’s run this in our browser and it will work like following.

You can find complete source code of this blog post at following location on Github-https://github.com/dotnetjalps/ASPNetCoreWebAPI
That’s it. Hope you like it. Stay tuned for more!


Post a Comment

Your feedback is very important to me. Please provide your feedback via putting comments.

Support this blog-Buy me a coffee

Buy me a coffeeBuy me a coffee
Search This Blog
Subscribe to my blog


My Mvp Profile
Follow us on facebook
Blog Archive
Total Pageviews