.NET

Cross-platform development platform - modern C#, high performance, enterprise-ready with excellent tooling

TL;DR

What: A free, cross-platform, open-source developer platform for building apps.

Why: Modern C#, high performance, unified platform, excellent tooling, enterprise-ready.

Quick Start

Install: Download from dotnet.microsoft.com

Create Web API:

dotnet new webapi -n MyApi
cd MyApi
dotnet run

Open https://localhost:5001/weatherforecast

Create minimal API:

dotnet new web -n MinimalApi
cd MinimalApi

Edit Program.cs:

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

app.MapGet("/", () => "Hello World!");
app.MapGet("/users/{id}", (int id) => new { Id = id });

app.Run();

Cheatsheet

CommandDescription
dotnet new webapiCreate Web API project
dotnet new webCreate minimal API
dotnet runRun the application
dotnet buildBuild the project
dotnet watch runRun with hot reload
dotnet add package NameAdd NuGet package
dotnet ef migrations addAdd EF migration

Gotchas

Minimal API

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();
app.UseSwagger();
app.UseSwaggerUI();

app.MapGet("/api/users", () => new[] { "Alice", "Bob" });
app.MapPost("/api/users", (User user) => Results.Created($"/api/users/{user.Id}", user));
app.MapPut("/api/users/{id}", (int id, User user) => Results.Ok(user));
app.MapDelete("/api/users/{id}", (int id) => Results.NoContent());

app.Run();

record User(int Id, string Name);

Controller-based API

[ApiController]
[Route("api/[controller]")]
public class UsersController : ControllerBase
{
    [HttpGet]
    public ActionResult<IEnumerable<User>> GetAll() => Ok(users);

    [HttpGet("{id}")]
    public ActionResult<User> Get(int id) => Ok(user);

    [HttpPost]
    public ActionResult<User> Create(User user) => CreatedAtAction(nameof(Get), new { id = user.Id }, user);
}

Dependency Injection

// Register service
builder.Services.AddScoped<IUserService, UserService>();

// Inject in controller
public class UsersController : ControllerBase
{
    private readonly IUserService _service;

    public UsersController(IUserService service)
    {
        _service = service;
    }
}

Next Steps