Create Simple Web API In ASP.NET MVC

Web API provides data transfer between data sources and applications. In this Web API Tutorial, learn how to create a simple C# Web API using ASP.NET.

Introduction

 
This is a step by step Web API tutorial that explains what Web API is, and how to create a simple Web API using ASP.NET MVC and C#. 
 

Web API Definition

 
Web API is an application programming interface (API) that is used to enable the communication or interaction with software components with each other. ASP.NET Web API is a framework that makes it easy to build HTTP Service that reaches a broad range of clients, including browsers and mobile devices. Using ASP.NET, web API can enable communicating by different devices from same database.
 
 
 
 
 

Uses of Web API

  • It is used to access service data in web applications as well as many mobile apps and other external devices.
  • It is used to create RESTful web services. REST stands for Representational State Transfer, which is an architectural style for networked hypermedia applications.
  • It is primarily used to build Web Services that are lightweight, maintainable, and scalable, and support limited bandwidth.
  • It is used to create simple HTTP Web Service. It supports XML, JSON, and other data formats.
Let's go through these step by step tutorial to create a simple Web API using ASP.NET MVC, C#, and Visual Studio.
 

Step 1. Create ASP.NET Web Application in Visual Studio

 
Open Visual Studio and open new project. Select Visual C# >> Web >> ASP.NET Web Application. After selecting all, give project name and click OK.
 
 
 

Step 2. Select Web API Template

 
Select Web API in template window. After selecting Web API, we can see some messages in the right side in template window. Now, click OK button.
 
 
 

Step 3. Review Project Files

 
Now, you're in Visual Studio with project files listed under the project name. We can see in the Solution Explorer in the right side with all important files and folders like MVC. The 
 
 
 

Step 4. Add a Controller

 
Now, go to Controller and expand the controller. Now, we can see “ValuesController.cs”. It is the main class for Web API. This controller is created by default. If we need a new controller or one with a different name, we can create that in the following way.
 
Select and right click Controllers >>Add >> Controller, just like the below screen.
 
 
 
Select “Web API 2 Controller - Empty” and click "Add" button from "Add Scaffold" window.
 
 
 
Here is what the screen looks like.
 
 
 
In Web API, Controller is inherited by “ApiController” abstract class. It is very important and basic for Web APIs. Namespace for this class is “System.Web.Http”.
 

Step 5. Add Controller Method

 
Now, create a simple method in Controller, build the application, and run it finally. In web API, we use method name as “Get ()". We can use any other method name. Let's write the below code in Demo controller. 
  1. using System.Net;  
  2. using System.Net.Http;  
  3. using System.Web.Http;  
  4. namespace WebAPI.Controllers {  
  5.     public class DemoController: ApiController {  
  6.         public string Get() {  
  7.             return "Welcome To Web API";  
  8.         }  
  9.         public List < string > Get(int Id) {  
  10.             return new List < string > {  
  11.                 "Data1",  
  12.                 "Data2"  
  13.             };  
  14.         }  
  15.     }  
  16. }   

URL Format to run Web API

 
We need to run Web API “api/Controller_Name” URL format. For example in MVC we define URL format using “RouteConfig” class and “RegisterRoutes” static methods. The same for Web API; we are using “WebApiConfig” and “Register” static method. Go to “Global.aspx” file we can see in detail.
 
 
 
We define the Web API URL format using “GlobalConfiguration” and “Configure” static method. We are passing “Register” method from “WebApiConfig” class as argument into “Configure” method. If we go to “WebApiConfig” class, we can see Web API routes..
 
 
 

Step 6

 
Now, build your project and run the above mentioned URL format. Our Controller name is “Demo”, so we will run the following
 
URL.“http://localhost:53027/api/Demo” .
 
Web API has returned a result in XML or JSON format. Our output looks like below.
 
If run http://localhost:53027/api/Demo, it will call “Get ()”. Since there is no parameter, the output looks like below.
 
 
 
If run http://localhost:53027/api/Demo/1, it will call “Get (int id)”. When there is a parameter in the method, the output looks like below.
Outputs return as XML format.
 
 
 

Conclusion

 
This article will help new learners, students, and freshers. In the next article, we learned how to create a Web API using ASP.NET, C#, and Visual Studio.