Program.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. using Microsoft.AspNetCore.Mvc;
  4. var app = WebApplication.Create(args);
  5. if (app.Environment.IsDevelopment())
  6. {
  7. app.UseDeveloperExceptionPage();
  8. }
  9. string Plaintext() => "Hello, World!";
  10. app.MapGet("/plaintext", Plaintext);
  11. object Json() => new { message = "Hello, World!" };
  12. app.MapGet("/json", Json);
  13. string SayHello(string name) => $"Hello, {name}!";
  14. app.MapGet("/hello/{name}", SayHello);
  15. app.MapGet("/null-result", IResult () => null);
  16. app.MapGet("/todo/{id}", Results<OkObjectHttpResult, NotFoundObjectHttpResult> (int id) =>
  17. {
  18. return id switch
  19. {
  20. >= 1 and <= 10 => (OkObjectHttpResult)Results.Ok(new { Id = id, Title = "Walk the dog" }),
  21. _ => (NotFoundObjectHttpResult)Results.NotFound()
  22. };
  23. });
  24. var extensions = new Dictionary<string, object>() { { "traceId", "traceId123" } };
  25. app.MapGet("/problem", () =>
  26. Results.Problem(statusCode: 500, extensions: extensions));
  27. app.MapGet("/problem-object", () =>
  28. Results.Problem(new ProblemDetails() { Status = 500, Extensions = { { "traceId", "traceId123" } } }));
  29. var errors = new Dictionary<string, string[]>();
  30. app.MapGet("/validation-problem", () =>
  31. Results.ValidationProblem(errors, statusCode: 400, extensions: extensions));
  32. app.MapGet("/validation-problem-object", () =>
  33. Results.Problem(new HttpValidationProblemDetails(errors) { Status = 400, Extensions = { { "traceId", "traceId123" } } }));
  34. app.Run();