mirror of
https://github.com/PacktPublishing/Learn-WinUI-3-Second-Edition.git
synced 2026-06-20 12:23:09 +00:00
Add starter project for Chapter 13
This commit is contained in:
+48
@@ -0,0 +1,48 @@
|
||||
namespace UnoMediaCollection.Server.Apis
|
||||
{
|
||||
internal static class WeatherForecastApi
|
||||
{
|
||||
private const string Tag = "Weather";
|
||||
private static readonly string[] Summaries = new[]
|
||||
{
|
||||
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
|
||||
};
|
||||
|
||||
internal static WebApplication MapWeatherApi(this WebApplication app)
|
||||
{
|
||||
app.MapGet("/api/weatherforecast", GetForecast)
|
||||
.WithTags(Tag)
|
||||
.WithName(nameof(GetForecast));
|
||||
return app;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a make believe weather forecast for the next 5 days.
|
||||
/// </summary>
|
||||
/// <param name="loggerFactory"></param>
|
||||
/// <returns>A fake 5 day forecast</returns>
|
||||
/// <remarks>A 5 Day Forecast</remarks>
|
||||
/// <response code="200">Weather Forecast returned</response>
|
||||
[Produces("application/json")]
|
||||
[ProducesResponseType(typeof(IEnumerable<WeatherForecast>), 200)]
|
||||
private static IEnumerable<WeatherForecast> GetForecast(ILoggerFactory loggerFactory)
|
||||
{
|
||||
var logger = loggerFactory.CreateLogger(nameof(WeatherForecastApi));
|
||||
logger.LogDebug("Getting Weather Forecast.");
|
||||
|
||||
return Enumerable.Range(1, 5).Select(index =>
|
||||
new WeatherForecast(
|
||||
DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
|
||||
Random.Shared.Next(-20, 55),
|
||||
Summaries[Random.Shared.Next(Summaries.Length)]
|
||||
)
|
||||
)
|
||||
.Select(x =>
|
||||
{
|
||||
logger.LogInformation("Weather forecast for {Date} is a {Summary} {TemperatureC}°C", x.Date, x.Summary, x.TemperatureC);
|
||||
return x;
|
||||
})
|
||||
.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
global using Microsoft.AspNetCore.Mvc;
|
||||
global using UnoMediaCollection.DataContracts;
|
||||
global using UnoMediaCollection.Server.Apis;
|
||||
@@ -0,0 +1,58 @@
|
||||
using Uno.Wasm.Bootstrap.Server;
|
||||
using UnoMediaCollection.DataContracts.Serialization;
|
||||
|
||||
try
|
||||
{
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// Configure the JsonOptions to use the generated WeatherForecastContext
|
||||
builder.Services.Configure<JsonOptions>(options =>
|
||||
options.JsonSerializerOptions.AddContext<WeatherForecastContext>());
|
||||
// Configure the RouteOptions to use lowercase URLs
|
||||
builder.Services.Configure<RouteOptions>(options =>
|
||||
options.LowercaseUrls = true);
|
||||
|
||||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen(c =>
|
||||
{
|
||||
// Include XML comments for all included assemblies
|
||||
Directory.EnumerateFiles(AppContext.BaseDirectory, "*.xml")
|
||||
.Where(x => x.Contains("UnoMediaCollection")
|
||||
&& File.Exists(Path.Combine(
|
||||
AppContext.BaseDirectory,
|
||||
$"{Path.GetFileNameWithoutExtension(x)}.dll")))
|
||||
.ToList()
|
||||
.ForEach(path => c.IncludeXmlComments(path));
|
||||
});
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI();
|
||||
}
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
|
||||
app.UseUnoFrameworkFiles();
|
||||
app.MapFallbackToFile("index.html");
|
||||
|
||||
app.MapWeatherApi();
|
||||
app.UseStaticFiles();
|
||||
|
||||
await app.RunAsync();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.Error.WriteLine("Application terminated unexpectedly");
|
||||
Console.Error.WriteLine(ex);
|
||||
#if DEBUG
|
||||
if (System.Diagnostics.Debugger.IsAttached)
|
||||
{
|
||||
System.Diagnostics.Debugger.Break();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
{
|
||||
"$schema": "https://json.schemastore.org/launchsettings.json",
|
||||
"iisSettings": {
|
||||
"windowsAuthentication": false,
|
||||
"anonymousAuthentication": true,
|
||||
"iisExpress": {
|
||||
"applicationUrl": "http://localhost:5000",
|
||||
"sslPort": 5001
|
||||
}
|
||||
},
|
||||
"profiles": {
|
||||
"UnoMediaCollection.Server": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "",
|
||||
"inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}",
|
||||
"applicationUrl": "https://localhost:5000;http://localhost:5001",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"IIS Express": {
|
||||
"commandName": "IISExpress",
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "",
|
||||
"inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\UnoMediaCollection.Wasm\UnoMediaCollection.Wasm.csproj" />
|
||||
<ProjectReference Include="..\UnoMediaCollection.DataContracts\UnoMediaCollection.DataContracts.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" />
|
||||
<PackageReference Include="Uno.Wasm.Bootstrap.Server" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
||||
Reference in New Issue
Block a user