Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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

DZone's Guide to

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 ·
Free Resource

Deploy code to production now. Release to users when ready. Learn how to separate code deployment from user-facing feature releases with LaunchDarkly.

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: 
  creating-aspnet-core-project 
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.
  creating-aspnet-core-api-project 
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()
            .UseKestrel()
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseIISIntegration()
            .UseStartup<Startup>()
            .Build();

            host.Run();
            }
    }
}
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: 
  creating-aspnet-core-api-controller-class 
 And here's the code for our get Method.

using System.Collections.Generic;
using CoreWebApi.Model;
using Microsoft.AspNetCore.Mvc;

namespace CoreWebApi.Controllers
{
    [Route("api/[controller]")]
    public class EmployeeController : Controller
    {
// GET: api/values
        [HttpGet]
        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: 

  aspnet-core-api-browser-sample 

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!

Deploy code to production now. Release to users when ready. Learn how to separate code deployment from user-facing feature releases with LaunchDarkly.

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

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}