- An Interview Question
Creating A Web API Using Visual Studio 2017
- Jun 22, 2022
- Other Artcile
In this tutorial, you'll learn how to create a Web API project using Visual Studio 2017.
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.
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.
After you click on OK, you will see the below window.
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.
Now, click on Controllers. Modify your HomeController as below.
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.
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).
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.
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.
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.
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.
- Creating Web API Project
- Visual Studio
- Visual Studio 2017
- Web API Project On Visual Studio
Printing in C# Made Easy
Mastering Visual Studio 2017 by Kunal Chowdhury
Get full access to Mastering Visual Studio 2017 and 60K+ other titles, with a free 10-day trial of O'Reilly.
There are also live events, courses curated by job role, and more.
Creating an ASP.NET Web Application
Once you are ready with creating an ASP.NET website and deploying it to Azure, open your Visual Studio 2017 instance. Create a new project by navigating to Templates | Visual C# | Cloud and selecting ASP.NET Web Application (.NET Framework), as shown in the following New Project dialog:
Give your project a name and click the OK button to start creating your web application from a template. The next screen will guide you to select the template that you want to use. There exists a number of templates ( Empty , Web Forms , MVC , Web API , Azure Mobile App , and so on) available for you to select.
We will select ...
Get Mastering Visual Studio 2017 now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.
Don’t leave empty-handed
Get Mark Richards’s Software Architecture Patterns ebook to better understand how to design components—and how they should interact.