Getting Started with ASP.NET Core
ASP.NET Core is a cross‑platform, high‑performance framework for building modern, cloud‑based, Internet‑connected applications. Whether you’re building web APIs, MVC web apps, or real‑time services, ASP.NET Core provides a streamlined development experience.
Why Choose ASP.NET Core?
- Cross‑platform: Run on Windows, macOS, and Linux.
- High performance: Built on Kestrel, a fast, lightweight web server.
- Unified programming model: Combine MVC, Web API, and Razor Pages.
- Modular: Add only the packages you need.
First Project – Hello World
Open a terminal and run the following commands:
dotnet new webapp -o MyFirstApp
cd MyFirstApp
dotnet run
Navigate to http://localhost:5000 in your browser. You should see the default Razor Pages template.
Key Files Explained
- Program.cs – Configures the host and sets up the request pipeline.
- Startup.cs – (Optional in .NET 6+) Contains service registration and middleware.
- appsettings.json – Stores configuration settings.
- Pages/Index.cshtml – The default Razor page rendered at the root URL.
Sample Controller
Below is a simple API controller returning JSON data:
using Microsoft.AspNetCore.Mvc;
namespace MyFirstApp.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing","Bracing","Chilly","Cool","Mild","Warm","Balmy","Hot","Sweltering","Scorching"
};
[HttpGet]
public IEnumerable<WeatherForecast> Get()
{
return Enumerable.Range(1,5).Select(index => new WeatherForecast
{
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
TemperatureC = Random.Shared.Next(-20,55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray();
}
}
public record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary);
}
Access it at http://localhost:5000/api/weatherforecast.
Comments