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..c1c7edbd6be2 --- /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("/", () => "Hello World!"); +// 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..549a5a44fe91 --- /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": "*" +}