DZone
Web Dev Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Web Dev Zone > How to Create a REST API (Web API) With ASP.NET Core 1.0

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!

Jalpesh Vadgama user avatar by
Jalpesh Vadgama
·
Jun. 15, 16 · Web Dev Zone · Tutorial
Like (4)
Save
Tweet
41.89K Views

Join the DZone community and get the full member experience.

Join For Free
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!
Web API ASP.NET ASP.NET Core REST Web Protocols

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

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • 10 Steps to Become an Outstanding Java Developer
  • Refactoring Java Application: Object-Oriented And Functional Approaches
  • Upsert in SQL: What Is an Upsert, and When Should You Use One?
  • Autowiring in Spring

Comments

Web Dev Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo