ASP.NET Web API - Start


In this tutorials, we will see the steps to set up your development environment and create a new project in ASP.NET Web API. Here is a step-by-step guide to help you get started.

Here we will cover the following.

  1. Prerequisites
  2. Create a New ASP.NET Web API Project
  3. Understand Project Structure
  4. Create a Web API Controller
  5. Impement API Actions
  6. Run the Web API

1. Prerequisites

  1. You need to install Visual Studio (2019 or later).
  2. You can download Visual Studio from the Microsoft website.
  3. You should familiar with C# programming language and the basics of ASP.NET.

2. Create a New ASP.NET Web API Project

  1. Now Open Visual Studio and click on Create a new project button.
    ASP.NET web API start
  2. In the project creation wizard, select ASP.NET Web Application and click Next button
    ASP.NET web API start
  3. Choose a project name and location and select API as the project template and ensure that .NET 5.0 (or the latest version) is selected as the target framework, then click Create button.
    ASP.NET web API start
  4. Click Create to generate the new Web API project.
    ASP.NET web API start

3. Understand Project Structure

The new project will have a structure similar to an ASP.NET Core application. Key files and folders include.

  1. Controllers - This folder contains the Web API controllers that handle incoming requests and produce responses.
  2. Models - This folder contains the data models used by the Web API.
  3. App_Start Folder -The WebApiConfig.cs is configuration file for Web API. You can configure routes and other things for web API, same like RouteConfig.cs is used to configure MVC routes.
ASP.NET web API start

4. Create a Web API Controller

  1. Inside the Controllers folder, right-click and select Add -> Controller.
    ASP.NET web API start
  2. Choose API Controller - Empty template and click Add.
  3. Give the controller a name, for example, UserController, and click Add.
  4. The new controller will be created with some default methods like Get, Post, Put, and Delete. You can modify and add more methods based on your Web API requirements.
    ASP.NET web API start

5. Impement API Actions

Within your controller, define the methods that will handle incoming HTTP requests and return responses. For example, a simple GET method might look like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace WebApiExample.Controllers
{
    public class UserController : ApiController
    {
        public class UserModel
        {
            public int Id { get; set; }
            public string Name { get; set; }
        }
        private static List users = new List()
        {
            new UserModel { Id = 1, Name = "Rohatash" },
            new UserModel { Id = 2, Name = "Rahul" },
            new UserModel { Id = 3, Name = "Deepak" }
        };

        // GET api/user
        public IHttpActionResult Get()
        {
            return Ok(users);
        }            
    }
}

6. Run the Web API

Press F5 or click on the Run button in Visual Studio to start the Web API project. The Web API will launch in your default web browser, and you should see an API page or a response depending on the actions you've implemented.

ASP.NET web API start
Next

Top Articles

  1. Why use C#
  2. How to use comment in C#
  3. How to use variables in C#
  4. How to use keywords in C#