Skip to content

Commit 557d324

Browse files
authored
feat: Migrate from Blazor Server to Blazor Web App (#447) fixes #368
1 parent 4ad0bd0 commit 557d324

File tree

5 files changed

+91
-23
lines changed

5 files changed

+91
-23
lines changed

src/LinkDotNet.Blog.Web/App.razor

Lines changed: 60 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,61 @@
1-
@using LinkDotNet.Blog.Web.Features.Home.Components
1+
@using Microsoft.AspNetCore.Hosting
2+
@using Microsoft.Extensions.Hosting
3+
@using Microsoft.Extensions.Options
24

3-
<CascadingAuthenticationState>
4-
<Router AppAssembly="@typeof(Program).Assembly">
5-
<Found Context="routeData">
6-
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)"/>
7-
</Found>
8-
<NotFound>
9-
<LayoutView Layout="@typeof(MainLayout)">
10-
<ObjectNotFound></ObjectNotFound>
11-
</LayoutView>
12-
</NotFound>
13-
</Router>
14-
</CascadingAuthenticationState>
5+
<!DOCTYPE html>
6+
<html lang="en" data-bs-theme="dark">
7+
<head>
8+
<meta charset="utf-8"/>
9+
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
10+
<base href="/"/>
11+
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png"/>
12+
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png"/>
13+
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png"/>
14+
<link rel="manifest" href="/site.webmanifest"/>
15+
<link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5"/>
16+
<link rel="alternate" type="application/rss+xml" title="@AppConfiguration.Value.BlogName" href="/feed.rss">
17+
<meta name="msapplication-TileColor" content="#da532c" />
18+
<meta name="theme-color" content="#ffffff" />
19+
<meta name="color-scheme" content="light dark" />
20+
<meta name="robots" content="max-image-preview:large" />
21+
<link rel="dns-prefetch" href="//cdnjs.cloudflare.com" />
22+
<link rel="preconnect" href="//cdnjs.cloudflare.com" />
23+
<link rel="preload" href="css/fonts/icons.woff2" as="font" type="font/woff2" crossorigin />
24+
<link rel="preload" href="css/fonts/icons.woff" as="font" type="font/woff" crossorigin />
25+
<link href="css/basic.css" rel="stylesheet" />
26+
<link href="css/icons.css" rel="stylesheet" />
27+
<link rel="preload" href="LinkDotNet.Blog.Web.styles.css" as="style">
28+
<link href="LinkDotNet.Blog.Web.styles.css" rel="stylesheet"/>
29+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.3.7/css/bootstrap.min.css" integrity="sha512-fw7f+TcMjTb7bpbLJZlP8g2Y4XcCyFZW8uy8HsRZsH/SwbMw0plKHFHr99DN3l04VsYNwvzicUX/6qurvIxbxw==" crossorigin="anonymous" referrerpolicy="no-referrer" />
30+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.11.1/styles/github-dark-dimmed.min.css" integrity="sha512-zcatBMvxa7rT7dDklfjauWsfiSFParF+hRfCdf4Zr40/MmA1gkFcBRbop0zMpvYF3FmznYFgcL8wlcuO/GwHoA==" crossorigin="anonymous" referrerpolicy="no-referrer" />
31+
<link href="_content/Blazorise/blazorise.css" rel="stylesheet" />
32+
<HeadOutlet />
33+
</head>
34+
<body>
35+
<Routes />
36+
<div id="blazor-error-ui">
37+
@if (Environment.IsDevelopment())
38+
{
39+
<text>An unhandled exception has occurred. See browser dev tools for details.</text>
40+
}
41+
else
42+
{
43+
<text>An error has occurred. This application may no longer respond until reloaded.</text>
44+
}
45+
<a href="/" class="reload">Reload</a>
46+
<a class="dismiss" role="button">x</a>
47+
</div>
48+
<script src="_framework/blazor.web.js"></script>
49+
<script async src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.11.1/highlight.min.js" integrity="sha512-EBLzUL8XLl+va/zAsmXwS7Z2B1F9HUHkZwyS/VKwh3S7T/U0nF4BaU29EP/ZSf6zgiIxYAnKLu6bJ8dqpmX5uw==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
50+
<script async src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.3.7/js/bootstrap.bundle.min.js" integrity="sha512-Tc0i+vRogmX4NN7tuLbQfBxa8JkfUSAxSFVzmU31nVdHyiHElPPy2cWfFacmCJKw0VqovrzKhdd2TSTMdAxp2g==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
51+
<script defer src="components/slideshow.js" ></script>
52+
</body>
53+
</html>
54+
55+
@code {
56+
[Inject]
57+
private IOptions<ApplicationConfiguration> AppConfiguration { get; set; } = default!;
58+
59+
[Inject]
60+
private IWebHostEnvironment Environment { get; set; } = default!;
61+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
@page "/NotFound"
2+
@using LinkDotNet.Blog.Web.Features.Components
3+
@using LinkDotNet.Blog.Web.Features.Home.Components
4+
@layout MainLayout
5+
6+
<PageTitle>Not Found</PageTitle>
7+
8+
<ObjectNotFound />

src/LinkDotNet.Blog.Web/Program.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ private static void RegisterServices(WebApplicationBuilder builder)
4949
.AddBlazoriseWithBootstrap()
5050
.AddResponseCompression()
5151
.AddHealthCheckSetup();
52+
53+
builder.Services.AddAntiforgery();
5254

5355
if (builder.Environment.IsDevelopment())
5456
{
@@ -84,18 +86,21 @@ private static void ConfigureApp(WebApplication app)
8486
})
8587
.RequireAuthorization();
8688

89+
app.UseStatusCodePagesWithReExecute("/NotFound");
90+
8791
app.UseRouting();
8892

8993
app.UseUserCulture();
9094

9195
app.UseAuthentication();
9296
app.UseAuthorization();
97+
98+
app.UseAntiforgery();
9399

94100
app.UseRateLimiter();
95101
app.MapControllers();
96-
app.MapBlazorHub();
97-
app.MapFallbackToPage("/_Host");
98-
app.MapFallbackToPage("/searchByTag/{tag}", "/_Host");
99-
app.MapFallbackToPage("/search/{searchTerm}", "/_Host");
102+
app.MapRazorPages();
103+
app.MapRazorComponents<App>()
104+
.AddInteractiveServerRenderMode();
100105
}
101106
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
@using LinkDotNet.Blog.Web.Features.Home.Components
2+
@using LinkDotNet.Blog.Web.Pages
3+
@using Microsoft.AspNetCore.Components.Web
4+
@rendermode RenderMode.InteractiveServer
5+
6+
<CascadingAuthenticationState>
7+
<Router AppAssembly="@typeof(Program).Assembly" NotFoundPage="@typeof(NotFound)">
8+
<Found Context="routeData">
9+
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
10+
</Found>
11+
</Router>
12+
</CascadingAuthenticationState>

src/LinkDotNet.Blog.Web/ServiceExtensions.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,8 @@ public static IServiceCollection AddBlazoriseWithBootstrap(this IServiceCollecti
6363
public static IServiceCollection AddHostingServices(this IServiceCollection services)
6464
{
6565
services.AddRazorPages();
66-
services.AddServerSideBlazor(s =>
67-
{
68-
#if DEBUG
69-
s.DetailedErrors = true;
70-
#endif
71-
});
66+
services.AddRazorComponents()
67+
.AddInteractiveServerComponents();
7268
services.AddSignalR(options =>
7369
{
7470
options.MaximumReceiveMessageSize = 1024 * 1024;

0 commit comments

Comments
 (0)