Skip to content

Commit 3bb4176

Browse files
authored
feat: support Blazor Server App (#169)
* feat(sample): add Blazor Server App * refactor(sample): use common AppOption * fix(core): inject `IConfiguration` instead of `IConfigurationRoot`
1 parent 8c8632b commit 3bb4176

28 files changed

+411
-108
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
using System.Runtime.CompilerServices;
22
using Microsoft.Extensions.Logging;
33

4-
namespace ConsoleAppExample;
5-
4+
#pragma warning disable CA1050, RCS1110
65
public partial class AppOption
76
{
8-
public DateTime LastLaunchedAt { get; set; }
7+
public DateTime LastChanged { get; set; }
8+
99
public string? ApiKey { get; set; }
10+
1011
public override string ToString()
11-
=> $"{{ {nameof(LastLaunchedAt)}: {LastLaunchedAt}, {nameof(ApiKey)} : {ApiKey} }}";
12+
=> $"{{ {nameof(LastChanged)}: {LastChanged}, {nameof(ApiKey)} : {ApiKey} }}";
1213

14+
#if NET6_0_OR_GREATER
1315
[LoggerMessage(0, LogLevel.Information, "{Name}: {Option}")]
1416
public static partial void LogInfomation(
1517
ILogger logger,
1618
AppOption option,
1719
[CallerArgumentExpression("option")] string? name = null);
20+
#endif
1821
}

sandbox/BlazorExample/App.razor

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Router AppAssembly="@typeof(App).Assembly">
2+
<Found Context="routeData">
3+
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
4+
<FocusOnNavigate RouteData="@routeData" Selector="h1" />
5+
</Found>
6+
<NotFound>
7+
<PageTitle>Not found</PageTitle>
8+
<LayoutView Layout="@typeof(MainLayout)">
9+
<p role="alert">Sorry, there's nothing at this address.</p>
10+
</LayoutView>
11+
</NotFound>
12+
</Router>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<Compile Include="../AppOption.cs" />
11+
<ProjectReference Include="../../src/Nogic.WritableOptions/Nogic.WritableOptions.csproj" />
12+
</ItemGroup>
13+
14+
</Project>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
@page
2+
@model BlazorExample.Pages.ErrorModel
3+
4+
<!DOCTYPE html>
5+
<html lang="en">
6+
7+
<head>
8+
<meta charset="utf-8" />
9+
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
10+
<title>Error</title>
11+
<link href="~/css/bootstrap/bootstrap.min.css" rel="stylesheet" />
12+
<link href="~/css/site.css" rel="stylesheet" asp-append-version="true" />
13+
</head>
14+
15+
<body>
16+
<div class="main">
17+
<div class="content px-4">
18+
<h1 class="text-danger">Error.</h1>
19+
<h2 class="text-danger">An error occurred while processing your request.</h2>
20+
21+
@if (Model.ShowRequestId)
22+
{
23+
<p>
24+
<strong>Request ID:</strong> <code>@Model.RequestId</code>
25+
</p>
26+
}
27+
28+
<h3>Development Mode</h3>
29+
<p>
30+
Swapping to the <strong>Development</strong> environment displays detailed information about the error that
31+
occurred.
32+
</p>
33+
<p>
34+
<strong>The Development environment shouldn't be enabled for deployed applications.</strong>
35+
It can result in displaying sensitive information from exceptions to end users.
36+
For local debugging, enable the <strong>Development</strong> environment by setting the
37+
<strong>ASPNETCORE_ENVIRONMENT</strong> environment variable to <strong>Development</strong>
38+
and restarting the app.
39+
</p>
40+
</div>
41+
</div>
42+
</body>
43+
44+
</html>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System.Diagnostics;
2+
using Microsoft.AspNetCore.Mvc;
3+
using Microsoft.AspNetCore.Mvc.RazorPages;
4+
5+
namespace BlazorExample.Pages;
6+
7+
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
8+
[IgnoreAntiforgeryToken]
9+
public class ErrorModel : PageModel
10+
{
11+
public string? RequestId { get; set; }
12+
13+
public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
14+
15+
public void OnGet() => RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier;
16+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
@page "/"
2+
3+
<PageTitle>Index</PageTitle>
4+
5+
@using Nogic.WritableOptions
6+
@inject IWritableOptions<AppOption> AppOption
7+
8+
<h1>Settings</h1>
9+
10+
<p role="status">
11+
API Key: @AppOption.CurrentValue.ApiKey <br />
12+
Last Changed: @AppOption.CurrentValue.LastChanged
13+
</p>
14+
15+
<button class="btn btn-primary" @onclick="UpdateSettings">Update</button>
16+
17+
@code {
18+
private void UpdateSettings()
19+
=> AppOption.Update(new()
20+
{
21+
ApiKey = Guid.NewGuid().ToString(),
22+
LastChanged = DateTime.Now
23+
}, true);
24+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
@page "/"
2+
@namespace BlazorExample.Pages
3+
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
4+
@{
5+
Layout = "_Layout";
6+
}
7+
8+
<component type="typeof(App)" render-mode="ServerPrerendered" />
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
@using Microsoft.AspNetCore.Components.Web
2+
@namespace BlazorExample.Pages
3+
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
4+
5+
<!DOCTYPE html>
6+
<html lang="en">
7+
8+
<head>
9+
<meta charset="utf-8" />
10+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
11+
<base href="~/" />
12+
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet"
13+
integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
14+
<link href="css/site.css" rel="stylesheet" />
15+
<link href="BlazorExample.styles.css" rel="stylesheet" />
16+
<component type="typeof(HeadOutlet)" render-mode="ServerPrerendered" />
17+
</head>
18+
19+
<body>
20+
@RenderBody()
21+
22+
<div id="blazor-error-ui">
23+
<environment include="Staging,Production">
24+
An error has occurred. This application may no longer respond until reloaded.
25+
</environment>
26+
<environment include="Development">
27+
An unhandled exception has occurred. See browser dev tools for details.
28+
</environment>
29+
<a href="" class="reload">Reload</a>
30+
<a class="dismiss">🗙</a>
31+
</div>
32+
33+
<script src="_framework/blazor.server.js"></script>
34+
</body>
35+
36+
</html>

sandbox/BlazorExample/Program.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using Nogic.WritableOptions;
2+
3+
var builder = WebApplication.CreateBuilder(args);
4+
5+
// Add services to the container.
6+
builder.Services.AddRazorPages();
7+
builder.Services.AddServerSideBlazor();
8+
builder.Services.ConfigureWritable<AppOption>(builder.Configuration.GetSection(builder.Environment.ApplicationName));
9+
10+
var app = builder.Build();
11+
12+
// Configure the HTTP request pipeline.
13+
if (!app.Environment.IsDevelopment())
14+
{
15+
_ = app.UseExceptionHandler("/Error")
16+
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
17+
.UseHsts();
18+
}
19+
20+
app.UseHttpsRedirection();
21+
22+
app.UseStaticFiles();
23+
24+
app.UseRouting();
25+
26+
app.MapBlazorHub();
27+
app.MapFallbackToPage("/_Host");
28+
29+
app.Run();
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
@inherits LayoutComponentBase
2+
3+
<PageTitle>BlazorExample</PageTitle>
4+
5+
<div class="page">
6+
<main>
7+
<div class="top-row px-4">
8+
<a href="https://docs.microsoft.com/aspnet/" target="_blank">About</a>
9+
</div>
10+
11+
<article class="content px-4">
12+
@Body
13+
</article>
14+
</main>
15+
</div>

0 commit comments

Comments
 (0)