General
Your first ASP.NET App: Controllers
0 views

Hello! In this tutorial we continue build your own ASP.NET Service!
Requirements
IDE like Visual Studio/JetBrains Rider
.NET >= 10
Base C# skill
Creating controller
Lets create our Controller, create file "MyController.cs"
Image will like this
namespace ExampleProject;
public class MyController
{
}
Let's set base configuration
using Microsoft.AspNetCore.Mvc;
namespace ExampleProject;
[Route("[controller]")]
[ApiController]
public class MyController : ControllerBase
{
}
Now let's move our endpoint here
remove
app.MapGet("/", () => "Hello World!");
from main file and add
[HttpGet]
public async Task<IActionResult> Get()
{
return Ok("Hello, world!");
}
to our controller
Now we need to add this controller to Program.cs
using Scalar.AspNetCore;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddOpenApi();
builder.Services.AddControllers();
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
app.MapScalarApiReference();
}
app.MapControllers();
app.Run();
Now let's run!
Open scalar at localhost:[your port]/scalar
press "Test Request" and press "Run"
And now we received "Hello, world!" from our controller
We can add more endpoints
like this
[HttpGet("{name}")]
public async Task<IActionResult> Get(string name)
{
var start = DateTime.Now;
await Task.Delay(1000);
var end = DateTime.Now;
return Ok($"Hello, {name}! It took {end - start} to respond.");
}
In next series we learn Databases and more!!!
Read original: https://dev.to/__f5cd865bec2/your-first-aspnet-app-controllers-5ck7
PreviousYour Fine-Grained GitHub Token Gets 404, Not 403. Check These Four Things Before the URL.NextYour First AWS Production Architecture: From a Simple Application to a Scalable System
Related posts
The Linkerd Story: The people Behind The Service Mesh — A new documentary premieres at KubeCon + CloudNativeCon North America 2026
0CNCF Blog
Londres en otoño: barrios, mercados y museos a tu ritmo
0dev.to Watercooler
Hello Tech & Marketing Community, In today's digital landscape, combining effective marketing strategies
0dev.to Tutorial
Man completes 28-year, 31,000-mile walk around world, returns to mother's house
0Hacker News New
Johnstown, PA will be your best place to live if you make under 100K
0Hacker News New
Cryosphere faces permanent damage when world overshoots 1.5C, scientists warn
0Hacker News New