Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move Weather page to the Blazor Web Client project when using WebAssembly-based interactivity #55877

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@
"rename": {
"BlazorWeb-CSharp/Components/Layout/": "./BlazorWeb-CSharp.Client/Layout/",
"BlazorWeb-CSharp/Components/Pages/Home.razor": "./BlazorWeb-CSharp.Client/Pages/Home.razor",
"BlazorWeb-CSharp/Components/Pages/Weather.razor": "./BlazorWeb-CSharp.Client/Pages/Weather.razor",
"BlazorWeb-CSharp/Components/Routes.razor": "./BlazorWeb-CSharp.Client/Routes.razor"
}
},
Expand All @@ -77,6 +76,20 @@
"BlazorWeb-CSharp.Client/Program.Main.cs"
]
},
{
"condition": "(WeatherForecastApi)",
"exclude": [
"BlazorWeb-CSharp/Components/Pages/Weather.razor"
]
},
{
"condition": "(!WeatherForecastApi)",
"exclude": [
"BlazorWeb-CSharp.Client/Pages/Weather.razor",
"BlazorWeb-CSharp.Client/Data/WeatherForecast.cs",
"BlazorWeb-CSharp/Data/WeatherForecastService.cs"
]
},
{
"condition": "(UseProgramMain)",
"exclude": [
Expand Down Expand Up @@ -122,7 +135,11 @@
"condition": "(!IndividualLocalAuth)",
"exclude": [
"BlazorWeb-CSharp/Components/Account/**",
"BlazorWeb-CSharp/Data/**",
"BlazorWeb-CSharp/Data/SqlLite/**",
"BlazorWeb-CSharp/Data/SqlServer/**",
"BlazorWeb-CSharp/Data/app.db/**",
"BlazorWeb-CSharp/Data/ApplicationDbContext.cs",
"BlazorWeb-CSharp/Data/ApplicationUser.cs",
"BlazorWeb-CSharp.Client/UserInfo.cs",
"BlazorWeb-CSharp.Client/Pages/Auth.razor"
]
Expand Down Expand Up @@ -407,6 +424,10 @@
"type": "computed",
"value": "(((IncludeSampleContent && (HostIdentifier != \"dotnetcli\" && HostIdentifier != \"dotnetcli-preview\"))) || ((!Empty && (HostIdentifier == \"dotnetcli\" || HostIdentifier == \"dotnetcli-preview\"))))"
},
"WeatherForecastApi": {
"type": "computed",
"value": "(UseWebAssembly && !UseServer && SampleContent)"
},
"AllInteractive": {
"type": "parameter",
"datatype": "bool",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace BlazorWeb_CSharp.Data;

public class WeatherForecast
{
public DateOnly Date { get; set; }

public int TemperatureC { get; set; }

public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);

public string? Summary { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
@page "/weather"
@*#if (!InteractiveAtRoot) -->
@rendermode @(new InteractiveWebAssemblyRenderMode(false))
##endif*@

@inject HttpClient Client

<PageTitle>Weather</PageTitle>

<h1>Weather</h1>

<p>This component demonstrates showing data.</p>

@if (forecasts == null)
{
<p><em>Loading...</em></p>
}
else
{
<table class="table">
<thead>
<tr>
<th>Date</th>
<th>Temp. (C)</th>
<th>Temp. (F)</th>
<th>Summary</th>
</tr>
</thead>
<tbody>
@foreach (var forecast in forecasts)
{
<tr>
<td>@forecast.Date.ToShortDateString()</td>
<td>@forecast.TemperatureC</td>
<td>@forecast.TemperatureF</td>
<td>@forecast.Summary</td>
</tr>
}
</tbody>
</table>
}

@code {
private WeatherForecast[]? forecasts;

protected override async Task OnInitializedAsync()
{
forecasts = await Client.GetFromJsonAsync<WeatherForecast[]?>("/api/weather");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ static async Task Main(string[] args)
builder.Services.AddCascadingAuthenticationState();
builder.Services.AddAuthenticationStateDeserialization();

#endif
#if (WeatherForecastApi)
// Register an HttpClient service to use for calling APIs on the server
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });

#endif
await builder.Build().RunAsync();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,9 @@
builder.Services.AddCascadingAuthenticationState();
builder.Services.AddAuthenticationStateDeserialization();

#endif
#if (WeatherForecastApi)
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });

#endif
await builder.Build().RunAsync();
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@
@using Microsoft.AspNetCore.Components.Web.Virtualization
@using Microsoft.JSInterop
@using BlazorWeb_CSharp.Client
@*#if (WeatherForecastApi)
@using BlazorWeb_CSharp.Data
##endif*@
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
<HeadOutlet @rendermode="InteractiveAuto" />
##elseif (UseServer)
<HeadOutlet @rendermode="InteractiveServer" />
##elseif (WeatherForecastApi)
<HeadOutlet @rendermode="@(new InteractiveWebAssemblyRenderMode(false))" />
##else
<HeadOutlet @rendermode="InteractiveWebAssembly" />
##endif*@
Expand All @@ -36,7 +38,7 @@
##elseif (UseServer)
<Routes @rendermode="InteractiveServer" />
##else
<Routes @rendermode="InteractiveWebAssembly" />
<Routes @rendermode="@(new InteractiveWebAssemblyRenderMode(false))" />
##endif*@
<script src="_framework/blazor.web.js"></script>
</body>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace BlazorWeb_CSharp.Data;

public class WeatherForecastService
{
private static readonly string[] Summaries =
[
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
];

public Task<WeatherForecast[]> GetWeatherForecastAsync()
{
return Task.FromResult(Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)],
}).ToArray());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
#endif
#if (UseWebAssembly && SampleContent)
using BlazorWeb_CSharp.Client.Pages;
#endif
using BlazorWeb_CSharp.Components;
#if (IndividualLocalAuth)
using BlazorWeb_CSharp.Components.Account;
#endif
#if (IndividualLocalAuth || WeatherForecastApi)
using BlazorWeb_CSharp.Data;
#endif

Expand Down Expand Up @@ -78,6 +77,10 @@ public static void Main(string[] args)

builder.Services.AddSingleton<IEmailSender<ApplicationUser>, IdentityNoOpEmailSender>();

#endif
#if (WeatherForecastApi)
builder.Services.AddScoped<WeatherForecastService>();

#endif
var app = builder.Build();

Expand Down Expand Up @@ -110,6 +113,10 @@ public static void Main(string[] args)
#endif
app.UseAntiforgery();

#if (WeatherForecastApi)
app.MapGet("/api/weather", async (WeatherForecastService wfs) => await wfs.GetWeatherForecastAsync());

#endif
app.MapStaticAssets();
#if (UseServer && UseWebAssembly)
app.MapRazorComponents<App>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
#endif
#if (UseWebAssembly && SampleContent)
using BlazorWeb_CSharp.Client.Pages;
#endif
using BlazorWeb_CSharp.Components;
#if (IndividualLocalAuth)
using BlazorWeb_CSharp.Components.Account;
#endif
#if (IndividualLocalAuth || WeatherForecastApi)
using BlazorWeb_CSharp.Data;
#endif

Expand Down Expand Up @@ -72,6 +71,10 @@

builder.Services.AddSingleton<IEmailSender<ApplicationUser>, IdentityNoOpEmailSender>();

#endif
#if (WeatherForecastApi)
builder.Services.AddScoped<WeatherForecastService>();

#endif
var app = builder.Build();

Expand Down Expand Up @@ -102,9 +105,12 @@
app.UseHttpsRedirection();

#endif

app.UseAntiforgery();

#if (WeatherForecastApi)
app.MapGet("/api/weather", async (WeatherForecastService wfs) => await wfs.GetWeatherForecastAsync());

#endif
app.MapStaticAssets();
#if (UseServer && UseWebAssembly)
app.MapRazorComponents<App>()
Expand Down
Loading