Creating Your First .NET Core Application

Beginner Updated January 20, 2025

๐Ÿงช 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.