ASP.NET Web API

Leave a comment

ASP.NET Web API allows us to build HTTP services. It can be used with ASP.NET MVC, Web Forms, WCF HttpBinding. The framework is built on ASP.NET.

We will go through an example of building a Web API with MVC using Entity Framework.

To create a new project, open Visual Studio and create a new ASP.NET Web Application:

Now select the type of project. Here we will select Web API:

The project is now created:

If we run the solution we get the following output:

With a link to API:

And help pages of how to call the API:

 

If we go to the API values URL /api/values:

We get the values returned:

Let’s add a model to our project from a database and have the framework create CRUD code for us. From the server explorer, right click Data Connections and select Add Connection:

Select the server and database. We will connect to the WideWorldImporters database:

Right click the Models folder and select Add->ADO.NET Entity Data Model:

Give the model a name:

Select EF Designer from database:

Choose the data connection:

Select the EF version:

Select the database objects to include in the model:

A model will be created with the objects selected:

Build the project.

Now add an ApiController.

Choose Web API 2 Controller with actions, using Entity Framework. Note – make sure to choose the Web API Controller:

Select the model and data context class:

Now, if you go to the help page, you will see the API has been updated to include Customers:

Browing to api/customers

Will display the list of customers:

You can then perform the other operations through the API.

 

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

Leave a Reply

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

thirteen + twelve =