ASP.NET Introduction
ASP.NET Core is a framework for building web applications and APIs with C#.
What is ASP.NET Core?
- It is made by Microsoft and is free and open source
- It runs on Windows, macOS and Linux
- You write the server code in C#
- It can build normal websites, web APIs, and real-time apps
- It is part of the .NET platform
ASP.NET vs ASP.NET Core
| Old ASP.NET (Framework) | ASP.NET Core | |
|---|---|---|
| Runs on | Windows only | Windows, macOS, Linux |
| Released | 2002 | 2016 |
| Speed | Good | Very fast |
| Status | Only maintained | Actively developed |
| Use for new projects | No | Yes |
When people say "ASP.NET" today, they usually mean ASP.NET Core. This tutorial teaches only the modern version.
Ways to Build Pages
ASP.NET Core gives you several styles. You will learn the two most useful for beginners:
- Razor Pages: each page is a
.cshtmlfile with its own C# code. Great for websites and forms. - Minimal APIs: small C# functions that answer URLs, usually with JSON. Great for mobile app back-ends.
There is also MVC (Models, Views, Controllers) for large apps, and Blazor for interactive pages written in C#.
Installing .NET
- Download the .NET SDK (version 8 or newer) from the official dotnet website.
- Install it like any other program.
- Open a terminal (Command Prompt, PowerShell or Terminal) and check it:
Example
dotnet --version8.0.400
Your version number may be different. For an editor, use Visual Studio (Windows) or VS Code with the C# Dev Kit extension.
Creating Your First App
Example
dotnet new web -o HelloApp
cd HelloApp
dotnet runBuilding...
info: Microsoft.Hosting.Lifetime[14]
Now listening on: http://localhost:5123
info: Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.Open the address shown (the port number will differ) in your browser, and you will see the text "Hello World!".
| Command | What it does |
|---|---|
dotnet new web | Creates an empty web app |
dotnet new webapp | Creates a Razor Pages app |
dotnet new webapi | Creates a web API project |
dotnet run | Builds and starts the app |
dotnet watch | Runs the app and reloads it when you save a file |
Use dotnet watch while learning. Every time you save, the browser updates and you see your change at once.
Changing the Message
Open Program.cs and change the text:
Example
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapGet("/", () => "Welcome to Workownix, Karachi!");
app.MapGet("/about", () => "We teach IT courses in Orangi Town.");
app.Run();Now / shows the first message and /about shows the second:
Welcome to Workownix, Karachi!
Exercise
Exercise
Which command creates a new Razor Pages project?
