Creating A Web API Project Using Visual Studio 2017

This article is an end to end flow of how to create a simple web API project using Visual Studio 2017. If you are a beginner and you do not know how to create a Web API project then this article will help you easily create Web API project having just basic C# knowledge.

Introduction
 
This article is an end-to-end flow of how to create a simple Web API project using Visual Studio 2017. If you are a beginner and you do not know how to create a Web API project, then this article will help you easily create a Web API project having just basic C# knowledge.
 
Steps for creating Web API using Visual Studio 2017
 
After you have opened Visual Studio, just click on File >> New >> Project. You will see something like below.
 
Creating Web API Project On Visual Studio 2017
 
For doing all of this, you can also use the shortcut "Ctrl+Shift+N".
 
Now, you will see the below pop-up window. Here, select Web and then select ASP.NET Web Application (.NET Framework). You can also create a Web API project using .NET Core which is given as another option but here, we will see it through the .NET Framework. Give your project a name. I have named it WebAPIDemo.
 
Creating Web API Project On Visual Studio 2017 
 
After you click on OK, you will see the below window.
 
Creating Web API Project On Visual Studio 2017
 
Here, in this window, you need to select Web API. And this is the place where you can choose the authentication method for your Web API. Since it is just a simple API, we will not use any authentication. Our API will be available to all. Otherwise, we could restrict it to individuals, Windows users, Office users, and there are also ways by which you can secure your Web API by OAuth authentication. I recently created a Web API project where we used OAuth2.
 
After doing all of the things mentioned above, just click OK and it will create your Web API project, which then, you can customize based on your requirements. Once the project is created and you click on Solution Explorer, you see the below project structure which comes for Web API template.
 
Creating Web API Project On Visual Studio 2017
 
Now, click on Controllers. Modify your HomeController as below.
  1. using System.Collections.Generic;  
  2. using System.Web.Http;  
  3. using WebAPIDemo.Models;  
  4.   
  5. namespace WebAPIDemo.Controllers  
  6. {  
  7.     public class HomeController : ApiController  
  8.     {  
  9.           
  10.   
  11.         public IEnumerable<Product> GetItems()  
  12.         {  
  13.             IList<Product> products = new List<Product>  
  14.             {  
  15.                 new Product  
  16.                 {  
  17.                     productName = "Biscuits",  
  18.                     manufacturingYear = 2018,  
  19.                     brandName="ParleG"  
  20.                 },  
  21.                 new Product  
  22.                 {  
  23.                     productName = "Cars",  
  24.                     manufacturingYear = 2018,  
  25.                     brandName="BMW"  
  26.                 },  
  27.                 new Product  
  28.                 {  
  29.                     productName = "Cars",  
  30.                     manufacturingYear = 2018,  
  31.                     brandName="Mercedese"  
  32.                 },  
  33.                 new Product  
  34.                 {  
  35.                     productName = "Brush",  
  36.                     manufacturingYear = 2017,  
  37.                     brandName="Colgate"  
  38.                 }  
  39.   
  40.             };  
  41.   
  42.             return products;  
  43.         }  
  44.     }  
  45.   
  46.       
  47. }  
We have written an API method which, when called, will return the list of products. Here, we can also write the logic to fetch these values from the database. For keeping it simple, I have used hardcoded values. I have returned a list of Product entity here. We will see later how to run this.
 
Before doing this, write a Product.cs class in Models which you can use in the Controller. For me, it was like following.
  1. namespace WebAPIDemo.Models  
  2. {  
  3.     public class Product  
  4.     {  
  5.         public string productName { getset; }  
  6.         public int manufacturingYear { getset; }  
  7.         public string brandName { getset; }  
  8.     }  
  9.       
  10. }  
I am telling you about the main files that you need to check before running this Web API. My WebApiConfig.cs file looks like this. This file is important because it will tell how we would be able to call our methods over the web. Technically, it configures the routes (paths to call Web APIs).
  1. public static class WebApiConfig  
  2.     {  
  3.         public static void Register(HttpConfiguration config)  
  4.         {  
  5.             // Web API configuration and services  
  6.   
  7.             // Web API routes  
  8.             config.MapHttpAttributeRoutes();  
  9.   
  10.             config.Routes.MapHttpRoute(  
  11.                 name: "DefaultApi",  
  12.                 routeTemplate: "api/{controller}/{action}/{id}",  
  13.                 defaults: new { id = RouteParameter.Optional }  
  14.             );  
  15.   
  16.         }  
  17.     }  
After this is done, we are ready to run our Web API project. Just build the project and debug. For debugging, you can press Ctrl+F5. It will open a browser window as below.
 
Creating Web API Project On Visual Studio 2017 
 
This is coming like this because we have not done anything for this route. To see the actual result, we need to change our URL according to the path given in WebApiConfig.cs. So, if you would do that, you will see the successful result in form of XML as below.
 
Creating Web API Project On Visual Studio 2017
 
You can see that we have created our Web API easily using VS 2017. Here, we have used the browser to hit this Web API in debugging mode but we can easily consume this Web API into other applications and see the result in the format we want.
 
Conclusion
 
Using the simple steps, we have learned here how to create a Web API project using Visual Studio 2017. We have covered end to end flow so that you do not face any difficulty. If you have any queries on this, do write in the comments section and also if it helps you somewhere, you can leave your feedback.