Creating Your First .NET Core Application
๐งช Creating Your First .NET Core Application
Let's build a simple "Hello World" console app:
dotnet new console -n HelloWorldApp
cd HelloWorldApp
dotnet run
You should see:
Hello, World!
๐ Project Structure and CLI Tools
Here's what the generated project looks like:
HelloWorldApp/
โโโ Program.cs
โโโ HelloWorldApp.csproj
Useful CLI Commands
dotnet build # Builds the project
dotnet run # Runs the project
dotnet test # Runs tests
dotnet publish # Publishes the app for deployment
๐ ASP.NET Core for Web Development
ASP.NET Core is the web framework built on top of .NET Core. It's used to build:
- RESTful APIs
- MVC Web Applications
- Blazor WebAssembly apps
Example: Creating a Web API
dotnet new webapi -n SampleAPI
cd SampleAPI
dotnet run
Open your browser at https://localhost:5001/weatherforecast
to see a sample API in action.
๐ Dependency Injection in .NET Core
.NET Core has built-in support for dependency injection (DI).
Example:
public interface IGreetingService
{
string Greet(string name);
}
public class GreetingService : IGreetingService
{
public string Greet(string name) => $"Hello, {name}!";
}
// In Program.cs or Startup.cs
builder.Services.AddScoped<IGreetingService, GreetingService>();
DI promotes testability, separation of concerns, and clean architecture.