Restful Web Services C# – ASP.NET Web API

ASP.NET Web API – In my previous blog ASP.NET Web Application using MVC I have created a simple project that explains how to create Restful Web Services(WEB API) using MVC default route. By this time I will create another project that will show you how to create ASP.NET API using API Controller class. This is the standard flow on creating API project in ASP.NET.

Let’s Start:

Step 1. First, open your visual studio and create new Project.

Restful Web Services

New Project

Step 2. Name your Project to your preference. In my case I name it as myRestAPI. Then press “OK”.

Restful Web Services

Name Project

Step 3. Select Empty from your template window shown below and check Web API from the checkbox below template option.

Restful Web Services

Select Empty Template

This is how your solution explorer looks for an Empty template project

Restful Web Services

Solution explorer

Step 4. Now add Web API Controller class inside your Controllers folder. To add right click on your Controller folder, select Add then choose New Item.

Restful Web Services

Add Web API Controller Class

Step 5. From the New Item window search for Web API Controller Class(v2) . Name your class controller then press Add.

Restful Web Services

Name Web API Controller

The image below is the API Controller Default route:

Restful Web Services

Web API URL Default Route

Step 6. Before we proceed with this file (MyAPController) let’s create an additional class inside Model folder. This will be used as a model to create our sample data later. To add, right click on your Model folder select Add then choose New Item.

Restful Web Services

Add Model Class

Step 7. Name your model. In my case, I name it as myModel.

Restful Web Services

Name Your Model

Step 8. Open myModel class then declare properties below.

Step 9. Open MyAPIController inside your Controller folder. If you name your API Controller same as mine you can copy and replace your code with the code below. If not manually create the method from the code below.

Method:

  • GetList() >> Return all data we created with our model.
  • GetName(int id) >> Return specific data by id.
  • Model >> Dummy list we create base on our myModel class.

Solution Explorer Overview – Restful Web Services

  • WebApiConfig >> contains default url route for Api Controller
  • myAPIController >> Api controller that contains our method
  • myModel >> Contains model data properties.
Restful Web Services

Solution Explorer

Step 10. To access GetList(). Run your project and navigate to this URL localhost:7831/api/myAPI

Restful Web Services

GetList()

Step 11. To access GetName(int id) >> localhost:7831/api/myAPI/GetName?id=1

Restful Web Services

GetName()

 

Code Summary:

MyAPIController

myModel:

From this moment you have now your own ASP.NET Web API. Thank you for reading.

Happy coding!!

 


Post you may like:

[pt_view id=”2384ae03it”]

Leave a Reply

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