Getting Started with ASP.NET Core
This guide will walk you through the initial steps of creating and running your first ASP.NET Core application. ASP.NET Core is a cross-platform, high-performance, open-source framework for building modern, cloud-enabled, internet-connected applications.
Prerequisites
Before you begin, ensure you have the following installed:
- .NET SDK: Download and install the latest .NET SDK from the official .NET website. The .NET SDK includes the .NET Runtime and the .NET CLI (Command-Line Interface).
- Code Editor: We recommend Visual Studio Code, Visual Studio, or JetBrains Rider.
Creating Your First ASP.NET Core Application
You can create an ASP.NET Core application using the .NET CLI. Open your terminal or command prompt and navigate to the directory where you want to create your project.
Using the .NET CLI
- Create a new project using the web API template:
dotnet new webapi -o MyWebApiApp
This command creates a new directory named
MyWebApiApp
and generates the project files inside it. - Navigate into the project directory:
cd MyWebApiApp
- Run the application:
dotnet run
The CLI will output the URL(s) where your application is listening, typically something like
https://localhost:5001
andhttp://localhost:5000
.
dotnet run
command usually prompts you if this is necessary. You can also run dotnet dev-certs https --trust
to manually trust it.
Running with Visual Studio
- Open Visual Studio.
- Select Create a new project.
- Search for "ASP.NET Core Web API" and select the template.
- Click Next.
- Enter a project name (e.g.,
MyWebApiApp
) and a location. - Click Next.
- Select the .NET version and configure additional settings as needed.
- Click Create.
- Once the project is created, press F5 or click the Run button to start the application.
Exploring the Project Structure
After creating the project, you'll find a basic structure that includes:
Program.cs
: The application's entry point. This file configures the web host and application pipeline.Controllers/WeatherForecastController.cs
: A sample API controller demonstrating how to handle incoming HTTP requests.appsettings.json
: Configuration settings for the application.Properties/launchSettings.json
: Settings for debugging and launching the application.
Making Your First API Call
With the application running, you can make an API call. If you used the default template, try accessing the weather forecast endpoint:
Open your web browser or use a tool like curl
or Postman and navigate to: https://localhost:[PORT]/weatherforecast
(replace [PORT]
with the port number shown when you ran dotnet run
, e.g., 5001).
You should see a JSON response containing an array of weather forecast objects.