# ASP. NET Web API Tutorial 01: Getting Started

Technologies
ASP.NET Web API
Topics
Web API
Platforms
Web
Requirements
Primary Language
en-US
Last Updated
5/31/2012
License
Apache License, Version 2.0
View this sample online
http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api

# Your First ASP.NET Web API (C#)

[Excerpts]
HTTP is not just for serving up web pages. It is also a powerful platform for building APIs that expose services and data. HTTP is simple, flexible, and ubiquitous. Almost any platform that you can think of has an HTTP library, so HTTP services can reach a broad range of clients, including browsers, mobile devices, and traditional desktop applications.
ASP.NET Web API is a framework for building web APIs on top of the .NET Framework. In this tutorial, you will use ASP.NET Web API to create a web API that returns a list of products.

## Adding a Controller

A controller is an object that handles HTTP requestst. The New Project wizard created two controllers for you when it created the project. To see them, expand the Controllers folder in Solution Explorer.

- `HomeController` is a traditional ASP.NET MVC controller. It is responsible for serving HTML pages for the site, and is not directly related to Web API.
- `ValuesController` is an example WebAPI controller.

Note If you have worked with ASP.NET MVC, then you are already familiar with controllers. They work similarly in Web API, but controllers in Web API derive from the ApiController class instead of Controller class. The first major difference you will notice is that actions on Web API controllers do not return views, they return data.

Go ahead and delete `ValuesController`, by right-clicking the file in Solution Explorer and selecting Delete.

Add a new controller, as follows:

In Solution Explorer, right-click the the Controllers folder. Select Add and then select Controller.

In the Add Controller wizard, name the controller "ProductsController". In the Template drop-down list, select Empty API Controller. Then click Add.

The Add Controller wizard will create a file named ProductsController.cs in the Controllers folder.

It is not necessary to put your contollers into a folder named Controllers. The folder name is not important; it is simply a convenient way to organize your source files.

If this file is not open already, double-click the file to open it. Add the following implementation:

C#
Edit|Remove
csharp

    namespace HelloWebAPI.Controllers
    {
        using HelloWebAPI.Models;
        public class ProductsController : ApiController
        {

            Product[] products = new Product[]
            {
                new Product { Id = 1, Name = "Soup", Category = "Groceries", Price = 1.39M },
                new Product { Id = 2, Name = "Yo-yo", Category = "Toys", Price = 3.75M },
                new Product { Id = 3, Name = "Hammer", Category = "Hardware", Price = 16.99M }
            };

            public IEnumerable<Product> GetAllProducts()
            {
                return products;
            }

            public Product GetProductById(int id)
            {
                var product = products.FirstOrDefault((p) => p.Id == id);
                if (product == null)
                {
                    var resp = new HttpResponseMessage(HttpStatusCode.NotFound);
                    throw new HttpResponseException(resp);
                }
                return product;
            }

            public IEnumerable<Product> GetProductsByCategory(string category)
            {
                return products.Where(
                    (p) => string.Equals(p.Category, category,
                        StringComparison.OrdinalIgnoreCase));
            }
        }
    }

    namespace HelloWebAPI.Controllers
    {
        using HelloWebAPI.Models;
        public class ProductsController : ApiController
        {

            Product[] products = new Product[]
            {
                new Product { Id = 1, Name = "Soup", Category = "Groceries", Price = 1.39M },
                new Product { Id = 2, Name = "Yo-yo", Category = "Toys", Price = 3.75M },
                new Product { Id = 3, Name = "Hammer", Category = "Hardware", Price = 16.99M }
            };

            public IEnumerable<Product> GetAllProducts()
            {
                return products;
            }

            public Product GetProductById(int id)
            {
                var product = products.FirstOrDefault((p) => p.Id == id);
                if (product == null)
                {
                    var resp = new HttpResponseMessage(HttpStatusCode.NotFound);
                    throw new HttpResponseException(resp);
                }
                return product;
            }

            public IEnumerable<Product> GetProductsByCategory(string category)
            {
                return products.Where(
                    (p) => string.Equals(p.Category, category,
                        StringComparison.OrdinalIgnoreCase));
            }
        }
    }