crear-y-configurar-proyecto-aspnet

How to Create a Project in ASP.NET

  • 4 min

In this article, we will see how to create and configure a project in ASP.NET, going through the initial steps that will allow us to start working with the technology.

Logically, this is the starting point: it doesn’t matter if you are taking your first steps or if you are preparing to launch a real project, the first thing is to create the project 😋.

Moreover, it will allow us to understand the structure, basic configuration, and functioning of an ASP.NET project.

Don’t worry if you feel a bit lost at first. It’s normal; it’s the first project. You’ll get the hang of it soon.

Prerequisites

Before installing and configuring ASP.NET, make sure you have the following installed on your system:

  1. .NET SDK: It’s the foundation for developing applications in .NET.
  2. An IDE or code editor: I recommend using Visual Studio 2022 or Visual Studio Code.
  3. Basic knowledge of C#: It’s not mandatory, but… yes, a little bit 😉

Create Your First ASP.NET Core Application

Creating an ASP.NET Core application is straightforward. For this, we can use tools like Visual Studio or the .NET CLI.

Simply run a command to create the project, depending on the type you want. For example, to create a new API project

dotnet new webapi -n MyFirstApp

Now change to the project directory,

cd MyFirstApp

Open Visual Studio and select Create a new project, and search for asp.net.

create-aspnet-app

Accept, and configure the project details.

What This Code Does

When we create our project, it will have a basic structure, with minimal code (so we don’t start from scratch).

Specifically, it is a very simple API made with ASP.NET that exposes an endpoint (/weatherforecast).

When a user accesses that route, the API returns a JSON with weather information for the next 5 days.

Let’s analyze the code of this base application because it contains what is necessary to understand how ASP.NET works.

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

var summaries = new[]
{
    "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};

app.MapGet("/weatherforecast", () =>
{
    var forecast =  Enumerable.Range(1, 5).Select(index =>
        new WeatherForecast
        (
            DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
            Random.Shared.Next(-20, 55),
            summaries[Random.Shared.Next(summaries.Length)]
        ))
        .ToArray();
    return forecast;
})
.WithName("GetWeatherForecast");

app.Run();

record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary)
{
    public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
}
  • ✅ Creates an API in C# with ASP.NET Core
  • ✅ Defines an endpoint /weatherforecast that returns a JSON with the weather for the next 5 days
  • ✅ Generates random data (date, temperature, and weather description)
  • ✅ ASP.NET automatically converts the response to JSON

Let’s look at each of these points in detail 👇,

The web application is created:

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
  • builder is the one who configures the application.
  • app is the application ready to run.

The route /weatherforecast is configured

app.MapGet("/weatherforecast", () =>
  • This means that if someone makes a GET request to /weatherforecast, the function defined there will be executed.

Each forecast is built using the WeatherForecast class

var forecast =  Enumerable.Range(1, 5).Select(index =>
new WeatherForecast
(
	DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
	Random.Shared.Next(-20, 55),
	summaries[Random.Shared.Next(summaries.Length)]
))
.ToArray();
return forecast;
  • The date is today + index days.
  • The temperature is random between -20 and 55 degrees.
  • A random weather summary is chosen.

Everything is converted into an array and returned as a response:**

return forecast;
  • Since no return type is specified, ASP.NET serializes it to JSON automatically by default.

ASP.NET Core, by default, uses automatic serialization. When a function returns an object or a list of objects, it converts it to JSON without us having to do anything extra.

Run

Now you can try running your application. To do this, simply run this command from the CLI.

dotnet run

Or press the ▶️ button if you are using Visual Studio.

In either case, you will see an output like the following.

aspnet-run

Now you can access this address from your browser (Chrome, Firefox, etc…),

http://localhost:5288/weatherforecast

And you will see the ASP.NET response with the weather forecast data (made up, of course, it’s an example) in JSON format.

aspnet-weatherforecast

It’s that simple! It’s not much more complicated to set up an API with ASP.NET. Now you can continue reading the rest of the course to master this technology 😉.