How to Build a Simple API in Visual Studio with ASP.Net C# – Tutorial 1

We would go through a simple tutorial on how to build an API in Visual Studio (Step by Step) using ASP.NET C#.

Before we go through this tutorial, let’s answer this two questions:

  • What is an API?
  • Why are APIs Important?

What is an API?

API stands for Application Programming Interface. API provides a set of tools needed for an application to communicate with another application.

For example, if you are building an application that would post to your Facebook page,  that application would be able to communicate with Facebook through Facebook APIs.

 

Why are APIs Important?

API allows programmers to build programs that communicate with other applications without having the implementation details of the target application.

Another uses is to allow application to manipulate the computer hardware by communicating with operating system.

APIs are also a secure way to access certain parts of applications without having access to the core of the application.

There are many other benefits of APIs as I would advice you read them up, so we can focus on building an API in Visual Studio.

 

Prerequisites

Visual Studio (2013, 2015 or 2017)

In this demo, we would build an API that returns the current date.

Step 1: Start an New Application in Visual Studio. Choose Web on the left and Select ASP.Net Web Application(.Net Framework) as shown in Figure 1. Give the application a name (MyAPI) and click OK.

Figure 1

Step 2: In the Screen shown, Select MVC fromt he list. Make sure the MVC and API checkboxes are checked as shown in Figure 2.

Figure 2

Step 3: Run the program and make sure is runs fine as shown in Figure 3. Now this is not an API yet. We are going to modify the application to be an API.

Figure 3

Step 4: In the Solution Explorer, right-click on the Controllers folder and select Add > Controller as shown in Figure 4.

Figure 4

Step 5: In the list of Items select, Web API 2 Controller – Empty  as shown in Figure 5

Figure 5

Step 6: The empty controller file is displayed as shown in Figure 6.

Figure 6

Step 7: Modify the DateController file by writing the following code inside the DateController Class.

        public string GetDate()
        {
            return DateTime.Now.ToShortDateString();
        }

Listing 1: Write this in between the curly braces.

After writing this code, your DateController file would now appear as shown in Figure 7.

Figure 7

Step 8: Test the API. We would test the API by making a GET request to the API endpoint. The API endpoint is /api/Date. We are going to discuss this in part two of this tutorial. To to this: Run the application. Then add /api/Date to the end of the url in the address bar. The date would be displayed in XML format as shown in Figure 8.

Figure 8

At this point, you have successfully built an API. In the next Tutorial, we would consider what makes up and API.

Do leave a comment you you have any challenges or if you find this informative.

 

Admin bar avatar

kindsonthegenius

Kindson Munonye is currently completing his doctoral program in Software Engineering in Budapest University of Technology and Economics

View all posts by kindsonthegenius →

Leave a Reply

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