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:

SOLVED: An unexpected error occurred in the .NET Framework Data Provider for MySQL

If you are here I’m expecting that you encountered same problem as mine. I was transferring a DataSet file to ...
Read More

How to create custom 404 error handler in ASP.NET MVC

Custom 404 error handler - 404 Error refer to a not found resource/URL . Here’s how a default 404 Error ...
Read More

AngularJS CRUD Operation using ASP.NET MVC

I am a .NET Developer and I’m new in AngularJS. I started using AngularJS in about a week ago and ...
Read More
SignalR Chat App With ASP.NET

SignalR Chat Application with ASP.NET MVC

SignalR Chat App With ASP.NET - This article will show you an actual application using SignalR in ASP.NET MVC. The ...
Read More
AgularJS

Submit Forms using AgularJS with ASP.NET MVC

Ways on how to submit forms using AngularJS: I’m new to AngularJS and I was trying to create a CRUD application ...
Read More
Self Signed Certificate

How: Creating Self Signed Certificate

As defined in sslshopper.com. A self-signed certificate is a certificate that is signed by the person creating it rather than a trusted ...
Read More
AngularJS

AngularJS with Asp.Net MVC

What is Angular? AngularJS is a JavaScript framework that extends HTML attribute to help develop a dynamic Web application. AngularJS ...
Read More

Importing Excel with ASP.NET MVC

Importing excel, In my previous article, I demonstrated how to perform simple way of downloading data into excels. Now, this ...
Read More

Export Data to Excel with ASP.NET MVC

In creating a system there are times that we need to export data to excel, it could either be for ...
Read More

Easy Steps to Implement Partial View in Asp.Net MVC

One of the best ways to implement a dynamic table without loading the whole page is by using Jquery Ajax ...
Read More

Leave a Reply

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