Getting Started with Web API


Getting Started with Web API

ASP.NET Web API is a framework for building web APIs on top of the .NET Framework. In this tutorial, we will use ASP.NET Web API to create a web API that returns a data in json format.

start visual studio and select New Project -> Choose Visual c# -> Web – > ASP.NET Web Application.

Give project Name and click on o.k.

ASP.NET Project

In New ASP.NET Project Dialog  Choose Empty Template   for Add folders and core references choose  MVC and WebAPI . 

Adding a Model

Now we will Add a model .Go to Model right click on Model -> choose Add-> New Item. Add New Item Dialog will open .From that dialog choose Data ->ADO.NET Entity Data model. Give Model Name and click on O.k.

Entity Data model Wizard will open and Ask for choose Model contents.choose EF Designer from Database and click on next.

Now it’s time for database connection.choose new collection and choose database for whish you want to create Web api and click on next .Model will added ton your Model folder.


Adding a Controller

In Web API, a controller is an object that handles HTTP requests. We’ll add a controller that can return the data in json format.

Right click on Controller -> Add Controller .Choose Web API 2 Controller with actions,using Entity Format and click on Add.


You will see your added Controller in controller folder.

Now we need to enable json cors.Go to Tools ->NuGet Package Manager ->Package Manager Console .

In the Package Manager Console window, type the following command:

Install-Package Microsoft.AspNet.WebApi.Cors

For getting you data in json format.add the following code in WebApiConfig.cs file.


config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue(“application/json”));

save and Rebuild the project . and Run It. You will see the Result below.


Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *