How to Set Up an ASP.NET Core Web API Project Locally (Step-by-Step)

How to Set Up an ASP.NET Core Web API Locally (Step-by-Step)

ASP.NET Core Web API is a powerful framework for building RESTful services using .NET. Setting up an ASP.NET Core Web API project locally is essential for development and testing. This guide walks you through the complete process, from installing the required tools to running and testing your APIs.

1. Install .NET SDK and CLI

Before creating an ASP.NET Core Web API, you need to install the .NET SDK (Software Development Kit) and CLI (Command-Line Interface).

1.1 Download and Install .NET SDK

  • Visit the official .NET download page.
  • Choose the latest stable version of the .NET SDK (Long-Term Support version recommended).
  • Install the SDK based on your operating system (Windows, macOS, or Linux).
  • Verify installation using the following command:
    dotnet --version
    
    This command displays the installed .NET SDK version.

1.2 Understanding .NET CLI

.NET CLI is a command-line interface for managing .NET projects. You can use it to create, build, and run ASP.NET Core applications.

  • Check available commands using:
    dotnet --help
    
  • Ensure .NET CLI is correctly installed and accessible from the terminal.

2. Set Up Visual Studio / VS Code for .NET Development

To develop an ASP.NET Core Web API, you need an Integrated Development Environment (IDE). You can choose between Visual Studio and Visual Studio Code (VS Code).

2.1 Install Visual Studio

  • Download and install Visual Studio.
  • During installation, select the ASP.NET and web development workload.
  • Ensure that the required .NET development components are installed.

2.2 Install Visual Studio Code

  • Download VS Code.
  • Install the C# extension from the VS Code marketplace.
  • Open VS Code and install the .NET CLI using:
    dotnet new tool-manifest
    dotnet tool install --global dotnet-aspnet-codegenerator
    

3. Create a Basic Web API Project

With the .NET SDK installed, you can create a new ASP.NET Core Web API project using .NET CLI.

3.1 Create a New Project

Open a terminal or command prompt and run:

dotnet new webapi -n MyWebAPI

This command creates a new Web API project named MyWebAPI.

3.2 Navigate to the Project Directory

cd MyWebAPI

3.3 Explore the Project Structure

Key project files include:

  • Program.cs – Entry point for the application.
  • Startup.cs (for older versions) – Contains configurations.
  • Controllers/ – Contains API controllers.
  • appsettings.json – Holds application settings.
  • WeatherForecastController.cs – A sample API controller.

4. Run and Test APIs Using Postman or Swagger UI

Once the project is set up, you can run and test your API endpoints.

4.1 Run the Web API

Execute the following command:

dotnet run

By default, the API runs on ports 5000 (HTTP) and 5001 (HTTPS).

4.2 Test Using Swagger UI

  • Open a web browser and navigate to https://localhost:5001/swagger.
  • Swagger UI displays all available API endpoints.
  • Click on an endpoint and test it by sending a request.

4.3 Test Using Postman

  • Install Postman.
  • Open Postman and create a new request.
  • Enter the API URL, e.g., https://localhost:5001/api/weatherforecast.
  • Click Send to see the response.

5. Enable CORS for Local Frontend Connections

CORS (Cross-Origin Resource Sharing) allows frontend applications running on different domains to access your API.

5.1 Add CORS Services in Program.cs

Modify Program.cs to include CORS support:

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddCors(options =>
{
    options.AddPolicy("AllowAll",
        policy => policy.AllowAnyOrigin()
                         .AllowAnyMethod()
                         .AllowAnyHeader());
});

var app = builder.Build();

app.UseCors("AllowAll");
app.UseAuthorization();
app.MapControllers();

app.Run();

5.2 Verify CORS Configuration

  • Restart the application using dotnet run.
  • Test API calls from a frontend app (React, Angular, Vue) hosted on a different domain.

Conclusion

Setting up an ASP.NET Core Web API project locally is straightforward with the right tools. 

By following this guide, you have: 

✅ Installed the .NET SDK and CLI. 

✅ Set up Visual Studio or VS Code for development. 

✅ Created a basic Web API project. 

✅ Tested APIs using Postman and Swagger UI. 

✅ Enabled CORS for frontend connections.

With this foundation, you can start building powerful APIs for your applications!

Sandip Mhaske

I’m a software developer exploring the depths of .NET, AWS, Angular, React, and digital entrepreneurship. Here, I decode complex problems, share insightful solutions, and navigate the evolving landscape of tech and finance.

Post a Comment

Previous Post Next Post