Our Blog

HTTP doesn’t just help up with making web pages. It is a robust platform for creating API’s and cultivating data. HTTP is very simple and adaptable. HTTP is virtually used everywhere and has a broad reach of different browsers, smartphones and desktops.
ASP.NET WEB API is a framework for building Web API’s on top of the .NET Framework. Right now we’re going to use ASP.NET Web API to create a Web API that returns a list of products.
Please download Visual Studio 2013 and Web API 2 if you haven’t already!

Creating a Web API Project

In this tutorial, we’re going to utilize ASP.NET Web app that returns a list of items. jQuery is used to display the results.

1. First, open Visual Studio. Select New Project from the Start page. You can alternatively open and click New Project from clicking the File menu.

2. Within the Templates pane, select Installed Templates and expand the Visual C# option. Then under Visual C#, select Web. Within the list of project templates, select ASP.NET Web Application. Name the project whatever you want to name it and then click OK.

3. Within the ASP.NET Project dialog box, select the Empty template option. Under “Add folders and core references for”, check off Web API. Then click OK.

How to add a Model

Models represent data in your application. ASP.NET Web API can tie your model to JSON, XML or other formats and write specific serialized data into the body of an HTTP response message. Most clients can desterilize objects and parse in either XLM or JSON. The client can also indicate whatever format it wants by setting the Accept header within the HTTP request message.

For example, let’s demonstrate a model right now!

1. Make sure that the Solution Explorer is visible before starting. If not, click the View menu and select Solution Explorer. Once you’re in Solution Explorer, right click the Models folder. Within the context menus, select Add, then select Class.

2. Afterwards, name your class whatever you want to name it. Add the below properties to the class that you named. In this instance, Wesson’s calls it ProductController.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ExampleApp.Models
{
    public class Movies
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Category { get; set; }
        public decimal Price { get; set; }
    }
}

Adding a Controller

In a Web API, controllers are objects that handle HTTP requests. Right now we’re going to add a controller that returns either a list of products or a particular product specified ID.

1. In the Solution Explorer, right click on the Controllers folder. Afterwards, select Add and then select Controller.

2. Within the Add Scaffold dialog box, select Web API Controller-Empty. Then click Add.

3. Once you see the Add Controller dialog box, label the Add Controller box and then click Add. Your file appears in the Controllers folder.

4. Once the file appears in the folder, add the following code below:

Congratulations! You have effectively created an API!

Pranayesh Pathak

One Comment

  1. lv eva clutch azur

    Good day! This is my first visit to your blog! We are a group of volunteers and starting a new initiative in a community in the same niche. Your blog provided us useful information to work on. You have done a outstanding job!

    Reply

So, what do you think ?