diff --git a/aspnetcore/fundamentals/minimal-apis.md b/aspnetcore/fundamentals/minimal-apis.md index b244663e4fb7..a8cdb439101c 100644 --- a/aspnetcore/fundamentals/minimal-apis.md +++ b/aspnetcore/fundamentals/minimal-apis.md @@ -799,12 +799,6 @@ The following code uses an [OpenAPI grouping tag](https://swagger.io/docs/specif [!code-csharp[](minimal-apis/samples/todo/Program.cs?name=snippet_grp)] - - :::moniker-end :::moniker range=">= aspnetcore-7.0" @@ -1618,6 +1612,14 @@ For more information, see +## Typed results + +The interface can represent values returned from minimal APIs that don’t utilize the implicit support for JSON serializing the returned object to the HTTP response. The static [Results](/dotnet/api/microsoft.aspnetcore.http.results) class is used to create varying `IResult` objects that represent different types of responses. For example, setting the response status code or redirecting to another URL. + +The types implementing `IResult` are public, allowing for type assertions when testing. For example: + +[!code-csharp[](~/fundamentals/minimal-apis/misc-samples/typedResults/TypedResultsApiWithTest/Test/WeatherApiTest.cs?name=snippet_1&highlight=7-8)] + ## OpenAPI An app can describe the [OpenAPI specification](https://swagger.io/specification/) for route handlers using [Swashbuckle](https://www.nuget.org/packages/Swashbuckle.AspNetCore/). The OpenAPI specification defines a standard for documenting RESTful APIs so that end users and other services can examine the capabilities of an API service. diff --git a/aspnetcore/fundamentals/minimal-apis/misc-samples/typedResults/TypedResultsApiWithTest/Program.cs b/aspnetcore/fundamentals/minimal-apis/misc-samples/typedResults/TypedResultsApiWithTest/Program.cs new file mode 100644 index 000000000000..62ebfae0c998 --- /dev/null +++ b/aspnetcore/fundamentals/minimal-apis/misc-samples/typedResults/TypedResultsApiWithTest/Program.cs @@ -0,0 +1,39 @@ +var builder = WebApplication.CreateBuilder(args); +var app = builder.Build(); + +app.MapGet("/", () => "Try /weatherforecast"); +// Map the /weatherforecast endpoint to a custom action. +app.MapWeatherApi(); +app.Run(); + +public static class WeatherApi +{ + public static string[] summaries = +{ + "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" +}; + + public static WebApplication MapWeatherApi(this WebApplication routes) + { + routes.MapGet("/weatherforecast", GetAllWeathers); + + return routes; + } + public static IResult GetAllWeathers() + { + var forecast = Enumerable.Range(1, 5).Select(index => + new WeatherForecast + ( + DateTime.Now.AddDays(index), + Random.Shared.Next(-20, 55), + summaries[Random.Shared.Next(summaries.Length)] + )) + .ToArray(); + return TypedResults.Ok(forecast); ; + } +} + +public record WeatherForecast(DateTime Date, int TemperatureC, string? Summary) +{ + public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); +} diff --git a/aspnetcore/fundamentals/minimal-apis/misc-samples/typedResults/TypedResultsApiWithTest/Test/WeatherApiTest.cs b/aspnetcore/fundamentals/minimal-apis/misc-samples/typedResults/TypedResultsApiWithTest/Test/WeatherApiTest.cs new file mode 100644 index 000000000000..b5335d68ff27 --- /dev/null +++ b/aspnetcore/fundamentals/minimal-apis/misc-samples/typedResults/TypedResultsApiWithTest/Test/WeatherApiTest.cs @@ -0,0 +1,18 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Microsoft.AspNetCore.Http.HttpResults; + +namespace Tests +{ + // + [TestClass()] + public class WeatherApiTests + { + [TestMethod()] + public void MapWeatherApiTest() + { + var result = WeatherApi.GetAllWeathers(); + Assert.IsInstanceOfType(result, typeof(Ok)); + } + } + // +} diff --git a/aspnetcore/fundamentals/minimal-apis/misc-samples/typedResults/TypedResultsApiWithTest/TypedResultsApiWithTest.csproj b/aspnetcore/fundamentals/minimal-apis/misc-samples/typedResults/TypedResultsApiWithTest/TypedResultsApiWithTest.csproj new file mode 100644 index 000000000000..32b3eb29c7a2 --- /dev/null +++ b/aspnetcore/fundamentals/minimal-apis/misc-samples/typedResults/TypedResultsApiWithTest/TypedResultsApiWithTest.csproj @@ -0,0 +1,16 @@ + + + + net7.0 + enable + enable + + + + + + + + + + diff --git a/aspnetcore/fundamentals/minimal-apis/misc-samples/typedResults/TypedResultsApiWithTest/appsettings.Development.json b/aspnetcore/fundamentals/minimal-apis/misc-samples/typedResults/TypedResultsApiWithTest/appsettings.Development.json new file mode 100644 index 000000000000..0c208ae9181e --- /dev/null +++ b/aspnetcore/fundamentals/minimal-apis/misc-samples/typedResults/TypedResultsApiWithTest/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/aspnetcore/fundamentals/minimal-apis/misc-samples/typedResults/TypedResultsApiWithTest/appsettings.json b/aspnetcore/fundamentals/minimal-apis/misc-samples/typedResults/TypedResultsApiWithTest/appsettings.json new file mode 100644 index 000000000000..10f68b8c8b4f --- /dev/null +++ b/aspnetcore/fundamentals/minimal-apis/misc-samples/typedResults/TypedResultsApiWithTest/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/aspnetcore/fundamentals/minimal-apis/misc-samples/typedResults/readme.txt b/aspnetcore/fundamentals/minimal-apis/misc-samples/typedResults/readme.txt deleted file mode 100644 index 655bdceab454..000000000000 --- a/aspnetcore/fundamentals/minimal-apis/misc-samples/typedResults/readme.txt +++ /dev/null @@ -1 +0,0 @@ -Create sample code here then delete this file. \ No newline at end of file