DEV Community

Cover image for Your first ASP.NET App: Introduction
Сабака Чабака
Сабака Чабака

Posted on

Your first ASP.NET App: Introduction

Hello! In this tutorial we will build your own ASP.NET Service!

Requirements

  • IDE like Visual Studio/JetBrains Rider
  • .NET >= 10
  • Base C# skill

Creating project

New Solution -> Web
Write your project name, as example - ExampleProject
Select Web API
Create and open Program.cs

First codes

When you created project and opened Program.cs, the image will like this

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
builder.Services.AddOpenApi();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.MapOpenApi();
}

app.UseHttpsRedirection();

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);
}
Enter fullscreen mode Exit fullscreen mode

Let's remove this all and leave only

using Scalar.AspNetCore;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddOpenApi();

var app = builder.Build();

if (app.Environment.IsDevelopment())
{
    app.MapOpenApi();
    app.MapScalarApiReference();
}

app.Run();
Enter fullscreen mode Exit fullscreen mode

For testing api we must add Scalar
dotnet add package Scalar.AspNetCore

And now, lets write our first code

app.MapGet("/", () => "Hello World!");
Enter fullscreen mode Exit fullscreen mode

Now run, and open in browser localhost:[your port]/scalar
Scroll down


Press [Test Request], and press [Send]


And now listened: Hello world

In next chapters we learn Controllers, Dependency Injection, Databases, Clean Architecture

Top comments (0)