Creating A Web API Using Visual Studio 2017

Introduction

This tutorial 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, this article will help you create a Web API project using simple steps and just with the 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

You may also use 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.

using System.Collections.Generic;  
using System.Web.Http;  
using WebAPIDemo.Models;  
  
namespace WebAPIDemo.Controllers  
{  
    public class HomeController : ApiController  
    {  
          
  
        public IEnumerable<Product> GetItems()  
        {  
            IList<Product> products = new List<Product>  
            {  
                new Product  
                {  
                    productName = "Biscuits",  
                    manufacturingYear = 2018,  
                    brandName="ParleG"  
                },  
                new Product  
                {  
                    productName = "Cars",  
                    manufacturingYear = 2018,  
                    brandName="BMW"  
                },  
                new Product  
                {  
                    productName = "Cars",  
                    manufacturingYear = 2018,  
                    brandName="Mercedese"  
                },  
                new Product  
                {  
                    productName = "Brush",  
                    manufacturingYear = 2017,  
                    brandName="Colgate"  
                }  
  
            };  
  
            return products;  
        }  
    }    
}

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.

namespace WebAPIDemo.Models  
{  
    public class Product  
    {  
        public string productName { get; set; }  
        public int manufacturingYear { get; set; }  
        public string brandName { get; set; }  
    }
} 

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).

public static class WebApiConfig  
{  
    public static void Register(HttpConfiguration config)  
    {  
        // Web API configuration and services  

        // Web API routes  
        config.MapHttpAttributeRoutes();  

        config.Routes.MapHttpRoute(  
            name: "DefaultApi",  
            routeTemplate: "api/{controller}/{action}/{id}",  
            defaults: new { id = RouteParameter.Optional }  
        );  

    }  
}

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.


Similar Articles