{"id":1958,"date":"2018-11-27T12:00:00","date_gmt":"2018-11-27T11:00:00","guid":{"rendered":"https:\/\/kindsonthegenius.com\/blog\/how-to-build-a-simple-api-in-visual-studio-with-asp-net-c-tutorial-1\/"},"modified":"2026-07-05T03:24:55","modified_gmt":"2026-07-05T01:24:55","slug":"how-to-build-a-simple-api-in-visual-studio-with-asp-net-c-tutorial-1","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/blog\/how-to-build-a-simple-api-in-visual-studio-with-asp-net-c-tutorial-1\/","title":{"rendered":"How to Build a Simple API in Visual Studio with ASP.Net C# \u2013 Tutorial 1"},"content":{"rendered":"<p>We would go through a simple tutorial on how to build an API in Visual Studio (Step by Step) using ASP.NET C#.<\/p>\n<p>Before we go through this tutorial, let&#8217;s answer this two questions:<\/p>\n<ul>\n<li>What is an API?<\/li>\n<li>Why are APIs Important?<\/li>\n<\/ul>\n<p><strong>What is an API?<\/strong><\/p>\n<p>API stands for Application Programming Interface. API provides a set of tools needed for an application to communicate with another application.<\/p>\n<p>For example, if you are building an application that would post to your Facebook page,\u00a0 that application would be able to communicate with Facebook through Facebook APIs.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Why are APIs Important?<\/strong><\/p>\n<p>API allows programmers to build programs that communicate with other applications without having the implementation details of the target application.<\/p>\n<p>Another uses is to allow application to manipulate the computer hardware by communicating with operating system.<\/p>\n<p>APIs are also a secure way to access certain parts of applications without having access to the core of the application.<\/p>\n<p>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.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Prerequisites<\/strong><\/p>\n<p>Visual Studio (2013, 2015 or 2017)<\/p>\n<p>In this demo, we would build an API that returns the current date.<\/p>\n<p><strong>Step 1:<\/strong> 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.<\/p>\n<figure id=\"attachment_76\" aria-describedby=\"caption-attachment-76\" style=\"width: 735px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-76 size-large\" src=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2020\/09\/How-to-Build-API-Figure-1-1024x584.jpg\" alt=\"\" width=\"735\" height=\"419\" \/><figcaption id=\"caption-attachment-76\" class=\"wp-caption-text\">Figure 1<\/figcaption><\/figure>\n<p><strong>Step 2:<\/strong> In the Screen shown, Select MVC fromt he list. Make sure the MVC and API checkboxes are checked as shown in Figure 2.<\/p>\n<figure id=\"attachment_77\" aria-describedby=\"caption-attachment-77\" style=\"width: 735px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-77 size-large\" src=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2020\/09\/How-to-Build-API-Figure-2-1024x668.jpg\" alt=\"\" width=\"735\" height=\"479\" \/><figcaption id=\"caption-attachment-77\" class=\"wp-caption-text\">Figure 2<\/figcaption><\/figure>\n<p><strong>Step 3:<\/strong> 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.<\/p>\n<figure id=\"attachment_78\" aria-describedby=\"caption-attachment-78\" style=\"width: 735px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-78 size-large\" src=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2020\/09\/How-to-Build-API-Figure-3-1024x544.jpg\" alt=\"\" width=\"735\" height=\"390\" \/><figcaption id=\"caption-attachment-78\" class=\"wp-caption-text\">Figure 3<\/figcaption><\/figure>\n<p><strong>Step 4:<\/strong> In the Solution Explorer, right-click on the Controllers folder and select Add &gt; Controller as shown in Figure 4.<\/p>\n<figure id=\"attachment_79\" aria-describedby=\"caption-attachment-79\" style=\"width: 735px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-79 size-large\" src=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2020\/09\/How-to-Build-API-Figure-4-1024x577.jpg\" alt=\"\" width=\"735\" height=\"414\" \/><figcaption id=\"caption-attachment-79\" class=\"wp-caption-text\">Figure 4<\/figcaption><\/figure>\n<p><strong>Step 5<\/strong>: In the list of Items select, Web API 2 Controller &#8211; Empty\u00a0 as shown in Figure 5<\/p>\n<figure id=\"attachment_80\" aria-describedby=\"caption-attachment-80\" style=\"width: 735px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-80 size-large\" src=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2020\/09\/How-to-Build-API-Figure-5-1024x688.jpg\" alt=\"\" width=\"735\" height=\"494\" \/><figcaption id=\"caption-attachment-80\" class=\"wp-caption-text\">Figure 5<\/figcaption><\/figure>\n<p><strong>Step 6:<\/strong> The empty controller file is displayed as shown in Figure 6.<\/p>\n<figure id=\"attachment_81\" aria-describedby=\"caption-attachment-81\" style=\"width: 735px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-81 size-large\" src=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2020\/09\/How-to-Build-API-Figure-6a-1024x698.jpg\" alt=\"\" width=\"735\" height=\"501\" \/><figcaption id=\"caption-attachment-81\" class=\"wp-caption-text\">Figure 6<\/figcaption><\/figure>\n<p><strong>Step 7:<\/strong> Modify the DateController file by writing the following code inside the DateController Class.<br \/>\n<!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%;\">        <span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #333399; font-weight: bold;\">string<\/span> <span style=\"color: #0066bb; font-weight: bold;\">GetDate<\/span>()\n        {\n            <span style=\"color: #008800; font-weight: bold;\">return<\/span> DateTime.Now.ToShortDateString();\n        }\n<\/pre>\n<\/div>\n<p>Listing 1: Write this in between the curly braces.<\/p>\n<p>After writing this code, your DateController file would now appear as shown in Figure 7.<\/p>\n<figure id=\"attachment_82\" aria-describedby=\"caption-attachment-82\" style=\"width: 735px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-82 size-large\" src=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2020\/09\/How-to-Build-API-Figure-7-1024x698.jpg\" alt=\"\" width=\"735\" height=\"501\" \/><figcaption id=\"caption-attachment-82\" class=\"wp-caption-text\">Figure 7<\/figcaption><\/figure>\n<p><strong>Step 8:<\/strong> 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.<\/p>\n<figure id=\"attachment_83\" aria-describedby=\"caption-attachment-83\" style=\"width: 735px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-83 size-large\" src=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2020\/09\/How-to-Build-API-Figure-8-1024x584.jpg\" alt=\"\" width=\"735\" height=\"419\" \/><figcaption id=\"caption-attachment-83\" class=\"wp-caption-text\">Figure 8<\/figcaption><\/figure>\n<p>At this point, you have successfully built an API. In the next Tutorial, we would consider what makes up and API.<\/p>\n<p>Do leave a comment you you have any challenges or if you find this informative.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"pagelayer_contact_templates":[],"_pagelayer_content":"","footnotes":""},"categories":[414],"tags":[],"class_list":["post-1958","post","type-post","status-publish","format-standard","hentry","category-programming"],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1958","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/comments?post=1958"}],"version-history":[{"count":1,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1958\/revisions"}],"predecessor-version":[{"id":2126,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1958\/revisions\/2126"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media?parent=1958"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/categories?post=1958"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/tags?post=1958"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}