How to Create a Web API using ASP.NET

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.

 

THANKS FOR READING. BEFORE YOU LEAVE, I NEED YOUR HELP.
 

I AM SPENDING MORE TIME THESE DAYS CREATING YOUTUBE VIDEOS TO HELP PEOPLE LEARN THE MICROSOFT POWER PLATFORM.

IF YOU WOULD LIKE TO SEE HOW I BUILD APPS, OR FIND SOMETHING USEFUL READING MY BLOG, I WOULD REALLY APPRECIATE YOU SUBSCRIBING TO MY YOUTUBE CHANNEL.

THANK YOU, AND LET'S KEEP LEARNING TOGETHER.

CARL

https://www.youtube.com/carldesouza

 

ABOUT CARL DE SOUZA

Carl de Souza is a developer and architect focusing on Microsoft Dynamics 365, Power BI, Azure, and AI.

carldesouza.comLinkedIn Twitter | YouTube

 

Leave a Reply

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