Over a million developers have joined DZone.

How to Create a REST API (Web API) With ASP.NET Core 1.0

Want to learn how to create a REST API with ASP.NET core 1.0? Look no further, you're in luck!

· Web Dev Zone

Start coding today to experience the powerful engine that drives data application’s development, brought to you in partnership with Qlik.

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

Let’s get started by creating an ASP.NET Core 1.0 Web Application like the following: 
Once you click on ASP.NET Web Application, it will ask whether you need to create Web Application or Web API application. We want Web API so I am going to select Web API Application like pictured below. Please note that in ASP.NET Core 1.0 there are no separate libraries or DLLs required for creating web APIs. This is just a project template.
Now, once you click OK It will create a Web API application with default values controller and program.cs. As you know, Program.cs is now the starting point for the ASP.NET Core 1.0 application, so it contains all the required configuration and startup items. Following is the 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 the 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 a web API controller by adding a new item like the following: 
 And here's 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 employees. Now, let’s run this in our browser, and it should work like the following: 


You can find the complete source code of this blog post at the following location on GitHub: https://github.com/dotnetjalps/ASPNetCoreWebAPI

That’s it. Hope you liked it. Stay tuned for more!

Create data driven applications in Qlik’s free and easy to use coding environment, brought to you in partnership with Qlik.

web dev,.net,rest api,.net core

Published at DZone with permission of Jalpesh Vadgama, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}