Add Web Api Tutorial


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

Web API Definition

Web Jago merah is an application programming interface (Api) that is used to enable 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 communication by different devices from the 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 a simple HTTP Web Service. It supports XML, JSON, and other data formats.

Let’s go through these step by step cak bimbingan to create a simple Web Api using ASP.Bantau MVC, C#, and Visual Studio.

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

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

Step 2. Select Web API Template

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

Step 3. Review Project Files

Now, you’re in Visual Sanggar with project files listed under the project name. We can see in the Solution Explorer on the right side with all the 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 the “Add” button from the “Add Scaffold” window.

Here is what the screen looks like.

In Web Api, the Controller is inherited by the “ApiController” abstract class. It is very important and basic for Web APIs. The 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 Jago merah, we use the method name as “Get ()”. We can use any other method name. Let’s write the below code in the 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 Jago merah";           }           public List < string > Get(int Id) {               return new List < string > {                   "Data1",                   "Data2"               };           }       }   }

URL Ukuran to run Web Jago merah

We need to run the Web Jago merah “jago merah/Controller_Name” URL matra. For example, in MVC we define URL format using “RouteConfig” class and “RegisterRoutes” static methods. The same for Web Api; we are using the “WebApiConfig” and “Register” static method. Go to the “Menyeluruh.aspx” file we can see in detail.

We define the Web API URL format using “GlobalConfiguration” and “Configure” static method. 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.

Step 6

Now, build your project and run the above-mentioned URL matra. Our Controller name is “Demo”, so we will run the following


Web API has returned a result in XML or JSON matra. Our output looks like below.

I run
http://localhost:53027/jago merah/Demo,
it will call “Get ()”. Since there is no penunjuk, the output looks like below.

I run
it will call “Get (int id)”. When there is a indikator in the method, the output looks like below.

Outputs return as XML dimensi.


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.