ASP.Net Web API Project Structure

ASP.Net Web API Project Structure

Welcome back. In this blog post, we will learn about the ASP.NET Web API project structure and how to create a simple web service using .NET 7.

What is ASP.NET Web API?

ASP.NET Web API is a framework that allows you to build web services that can communicate with various clients, such as browsers, mobile devices, or desktop applications. Web services are applications that expose data and functionality over the internet using standard protocols, such as HTTP. Web services can use different formats to exchange data, such as XML, JSON, or plain text.

ASP.NET Web API is based on the MVC (Model-View-Controller) pattern, which separates the application into three components: model, view, and controller. The model represents the data and business logic of the application. The view renders the data and user interface of the application. The controller handles the requests and responses of the application.

However, unlike ASP.NET MVC, which is mainly used for building web pages, ASP.NET Web API does not have a view component. Instead, it focuses on creating HTTP services that return data in various formats. The controller component in ASP.NET Web API is responsible for defining the endpoints or routes of the web service and implementing the actions or methods that handle the requests and responses.

ASP.NET Web API project structure

To create an ASP.NET Web API project, you need to have Visual Studio 2022 or later installed on your machine. You also need to have .NET 7 SDK installed on your machine. You can download them from here: https://visualstudio.microsoft.com/downloads/ and https://dotnet.microsoft.com/download/dotnet/7.0.

To create an ASP.NET Web API project, follow these steps:

  • Open Visual Studio and select Create a new project.

  • In the Create a new project window, search for ASP.NET Core Web Application and select it. Then click Next.

  • In the Configure your new project window, enter a name for your project, such as MyWebApi. You can also change the location and solution name if you want. Then click Next.

  • In the Additional information window, select .NET 7.0 as the target framework. Then click Next.

  • In the Create a new ASP.NET Core Web Application window, select API as the project template. You can also check or uncheck other options if you want. Then click Create.

After creating the project, you will see the following files and folders in the Solution Explorer:

  • appsettings.json: This file contains the configuration settings for your application, such as connection strings, logging options, etc.

  • Program.cs: This file contains the entry point of your application, which creates and runs a web host that hosts your web service. This file also contains the configuration of your web host, such as adding services to the dependency injection container, configuring middleware components in the request pipeline, etc.

  • Controllers: This folder contains the controller classes that define the endpoints and actions of your web service.

  • WeatherForecast.cs: This file contains a model class that represents a weather forecast data.

  • WeatherForecastController.cs: This file contains a controller class that inherits from ControllerBase and has a [ApiController] attribute and a [Route(“api/[controller]”)] attribute. This class defines a GET method that returns an array of weather forecast data in JSON format.

ASP.NET Web API example

To test your web service, you can use Postman or any other tool that can send HTTP requests and receive HTTP responses. You can also use a browser to test GET requests.

To test your web service using Postman, follow these steps:

  • Open Postman and create a new request tab.

  • In the request tab, select GET as the method and enter https://localhost:5001/api/weatherforecast (port may be different for your application, please check the same in launchsettings.json) as the URL. This is the default URL of your web service endpoint that returns weather forecast data.

  • Click Send and see the response in JSON format in the response body section.

You should see something like this:

[
    {
        "date": "2023-06-05T02:25:06.000Z",
        "temperatureC": 30,
        "temperatureF": 86,
        "summary": "Hot"
    },
    {
        "date": "2023-06-06T02:25:06.000Z",
        "temperatureC": 28,
        "temperatureF": 82,
        "summary": "Warm"
    },
    {
        "date": "2023-06-07T02:25:06.000Z",
        "temperatureC": 26,
        "temperatureF": 78,
        "summary": "Cool"
    },
    {
        "date": "2023-06-08T02:25:06.000Z",
        "temperatureC": 24,
        "temperatureF": 75,
        "summary": "Mild"
    },
    {
        "date": "2023-06-09T02:25:06.000Z",
        "temperatureC": 22,
        "temperatureF": 71,
        "summary": "Chilly"
    }
]

This is the default data that is generated by the WeatherForecastController class. You can modify this class or create new classes to implement your own logic and return your own data.

This post should have helped you get to know about ASP.NET Web API project structure and how to create a simple web service using .NET 7.

Did you find this article valuable?

Support Vignesh Ponnuvel by becoming a sponsor. Any amount is appreciated!