Updated March 30, 2023
Introduction to ASP.NET Web API
ASP.NET Web API is a perfect platform for creating restful services and supports for request and response pipeline. ASP.NET Web API is an extensible framework that helps build an HTTP service that can be accessed on various platforms like windows, web, mobile, and so on. It is like a WebService supports only the HTTP Protocols. Web API is an interface which provides the communication between software applications. It can be hosted in self-hosted, IIS or other web servers which supports .NET 4.0+.
What is Web API in ASP.NET?
- Web API (Application Programming Interface) in ASP.NET is an extensible framework for creating the HTTP (Hypertext Transfer Protocol) services which can be right to use from any of the clients such as web browsers, desktop applications, mobile devices and IOTs (Internet of Things).
- The Web API services are used once the applications are on a distributed system. Web API receives the requests from various types of client-side devices like laptop, mobiles and send back the responses to the web-server and then finally returns with the data to the desired client.
How to Create Web API in ASP.NET?
Open the Visual Studio and select the File->New Project as shown in the image.
Select the ASP.NET Web MVC Web application in a new project, name your project as your wish, and then click ok as shown in the image.
Once you selected the ok button, the new window will appear in the name of the New ASP.NET Project; from that template, select Web API project template and click on the ok button as shown in the image.
The folder structures of Web API application:
Look at your solution explorer on the right top side; there are important files and folders listed; they are models, views and controllers; inside the controller’s folder, there will be MVC controllers and Web API controllers.
The essential thing is that Web API Controllers are different from MVC Controllers here; ValueController is the WebAPI Controller.
Let’s look at the ValuesController it contains various methods like Get, Post, Delete and put, which are mapped to the HTTP verbs GET, POST, DELETE and PUT.
Code:
public class ValuesController : ApiController
{
// GET api/values
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
// GET api/values/5
public string Get(int id)
{
return "value";
}
// POST api/values
public void Post([FromBody]string value)
{
}
// PUT api/values/5
public void Put(int id, [FromBody]string value)
{
}
// DELETE api/values/5
public void Delete(int id)
{
}
}
This above code contains two overloaded methods; one is Get() method, which does not contain any parameter within it, whereas another method is with the id parameter. It will response for the GET HTTP based on the passing parameter of id. There is a default router for Web API; it have the Application_Start() method within the Global.asax. It will execute when the application starts at the first time. The Application_Start() method contains the configuration for Filters, Bundles and so on.
We define the Web API URL format using the “GlobalConfiguration” and “Configure” static methods. We are passing the “Register” method from the “WebApiConfig” class as an argument into the “Configure” method. If we go to the “WebApiConfig” class, we can see Web API routes.
In the APP_Start folder, there will be a routing configuration based on that only the application executes. In the Web API project, you can see the default route configured with the Register () method. We can see the Web API routing in the “WebApiConfig” class. The routing is ordered by which the Web API starts with the word API followed by / and then the name of the controller and then one more / and the parameter id.
Code:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
Why to Use Web API?
We are moving to Web API because nowadays, customers are not satisfied with Web-Based Applications alone they are going to upgrade day-to-day with mobiles, tablets, iPhones, etc. So, in reality, we are moving towards from the Web to the apps world. So we are using Web service data to the browsers and the other devices easily that’s the Web API, which is well-suited with browsers and other devices.
Web API is the perfect framework to depict the data and service to various devices; Web API is an open-source platform to create the REST-ful services. Web API uses the entire HTTP features like request/ response headers, versioning, different content-formats, caching, and you need not define additional settings like configurations for various devices, unlike WCF Rest-Services. ASP.NET Web API is a framework for building HTTP services that can be consumed by a broad range of clients, including browsers, mobiles, iphone and tablets. It can be used with ASP.NET MVC and other types of Web applications like ASP.NET WebForms. Also, Web API can be used as a stand-alone Web services application.
ASP.NET Web API is an extensible framework that helps build an HTTP service that can be accessed on various platforms like windows, web, mobile, and so on. The Web API can be used with ASP.NET MVC, and other Web Applications like ASP.Net WebForms and also the Web API is used like a stand-alone Web Service Application. The API is similar to the online Web Services, which is used by the apps at the client-side to receive and to updating the information. An API acts like the vital role of an application without knowing the number of client apps that make use of it.
Examples of ASP.NET Web API
Different examples are mentioned below:
1. Open the Visual Studio and select the File->New Project as shown in the image.
2. Select the Web API template.
3. Look at the folder structures once the project is created.
4. Add a controller and code like your desire logic.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
namespace api_web.Controllers
{
public class ValuesController : ApiController
{
// GET api/values
public string Get()
{
return "Welcome To ASP.NET Web API";
}
}
}
5. As said earlier in the creation of the application, look at the routing configuration in the APP_Start folder of the “WebApiConfig” class.
6. Now build and run the application; here, our controller name is “Values”.
So it will execute in the following format as shown below:
URL: http://localhost:54352//api/values
Web API returns output in the format of XML or JSON format; our result will be as follows.
Conclusion
We used Web API to build the Web-Services light-weighted and maintainable; it helps to access the service data from various internet devices. In this article, we have seen how important the Web API is and how to create a Web API in ASP.NET application, with example.
Recommended Articles
We hope that this EDUCBA information on “ASP.NET Web API” was beneficial to you. You can view EDUCBA’s recommended articles for more information.