Introduction
This article explains about Web API and how to create a simple Web API with ASP.NET Web API in an easy way.
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.
Diagram
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.
Steps for creating Web API
Step 1
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 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
Our project will open. We can see the Solution Explore right side with all important files and folders like MVC. If we select Empty, the application will open with empty template. But, we did not select empty application.
Step 4
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
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.
- using System.Net;
- using System.Net.Http;
- using System.Web.Http;
- namespace WebAPI.Controllers {
- public class DemoController: ApiController {
- public string Get() {
- return "Welcome To Web API";
- }
- public List < string > Get(int Id) {
- return new List < string > {
- "Data1",
- "Data2"
- };
- }
- }
- }
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 will learn how to use Web API in Web Forms in MVC.