.NET

Plattformubergreifende Entwicklungsplattform - modernes C#, hohe Leistung, Enterprise-ready

TL;DR

Was: Eine kostenlose, plattformübergreifende, Open-Source-Entwicklerplattform zum Erstellen von Apps.

Warum: Modernes C#, hohe Leistung, vereinheitlichte Plattform, exzellente Tools, Enterprise-ready.

Quick Start

Installation: Download von dotnet.microsoft.com

Web API erstellen:

dotnet new webapi -n MyApi
cd MyApi
dotnet run

Öffne https://localhost:5001/weatherforecast

Minimale API erstellen:

dotnet new web -n MinimalApi
cd MinimalApi

Program.cs bearbeiten:

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

BefehlBeschreibung
dotnet new webapiWeb API-Projekt erstellen
dotnet new webMinimale API erstellen
dotnet runAnwendung ausführen
dotnet buildProjekt bauen
dotnet watch runMit Hot Reload ausführen
dotnet add package NameNuGet-Paket hinzufügen
dotnet ef migrations addEF-Migration hinzufügen

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

// Service registrieren
builder.Services.AddScoped<IUserService, UserService>();

// Im Controller injizieren
public class UsersController : ControllerBase
{
    private readonly IUserService _service;

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

Next Steps