From 77d81c31b0a9761e690c17ca8a7cffb725ede0c1 Mon Sep 17 00:00:00 2001 From: Maximilian Mantz Date: Tue, 7 May 2024 14:54:27 +0200 Subject: [PATCH] migrated to Blazor web app --- samples/BlazorServer/App.razor | 61 +++++++++++++---------- samples/BlazorServer/HostingExtensions.cs | 9 ++-- samples/BlazorServer/Pages/Index.razor | 25 +++++++++- samples/BlazorServer/Pages/_Host.cshtml | 8 --- samples/BlazorServer/Pages/_Layout.cshtml | 32 ------------ samples/BlazorServer/Routes.razor | 25 ++++++++++ samples/BlazorServer/_Imports.razor | 3 +- 7 files changed, 92 insertions(+), 71 deletions(-) delete mode 100644 samples/BlazorServer/Pages/_Host.cshtml delete mode 100644 samples/BlazorServer/Pages/_Layout.cshtml create mode 100644 samples/BlazorServer/Routes.razor diff --git a/samples/BlazorServer/App.razor b/samples/BlazorServer/App.razor index 3127ca9..fcb53a3 100644 --- a/samples/BlazorServer/App.razor +++ b/samples/BlazorServer/App.razor @@ -1,25 +1,36 @@ - - - - - - @if (context.User.Identity?.IsAuthenticated != true) - { - - } - else - { -

You are not authorized to access this resource.

- } -
-
- -
- - Not found - -

Sorry, there's nothing at this address.

-
-
-
-
\ No newline at end of file +@inject IHostEnvironment Env + + + + + + + + + + + + + + +
+ @if (Env.IsDevelopment()) + { + + An unhandled exception has occurred. See browser dev tools for details. + + } + else + { + + An error has occurred. This app may no longer respond until reloaded. + + } + Reload + 🗙 +
+ + + + + diff --git a/samples/BlazorServer/HostingExtensions.cs b/samples/BlazorServer/HostingExtensions.cs index 3bcb7f8..e6a5859 100644 --- a/samples/BlazorServer/HostingExtensions.cs +++ b/samples/BlazorServer/HostingExtensions.cs @@ -1,6 +1,7 @@ using BlazorServer.Plumbing; using BlazorServer.Services; using Serilog; +using BlazorServer; namespace BlazorServer; @@ -65,7 +66,8 @@ public static WebApplication ConfigureServices(this WebApplicationBuilder builde builder.Services.AddControllersWithViews(); builder.Services.AddRazorPages(); - builder.Services.AddServerSideBlazor(); + builder.Services.AddRazorComponents() + .AddInteractiveServerComponents(); builder.Services.AddSingleton(); @@ -79,13 +81,14 @@ public static WebApplication ConfigurePipeline(this WebApplication app) app.UseStaticFiles(); app.UseRouting(); + app.UseAntiforgery(); app.UseAuthentication(); app.UseAuthorization(); app.MapDefaultControllerRoute(); - app.MapBlazorHub(); - app.MapFallbackToPage("/_Host"); + app.MapRazorComponents() + .AddInteractiveServerRenderMode(); return app; } diff --git a/samples/BlazorServer/Pages/Index.razor b/samples/BlazorServer/Pages/Index.razor index d1c2757..b189d87 100644 --- a/samples/BlazorServer/Pages/Index.razor +++ b/samples/BlazorServer/Pages/Index.razor @@ -1,9 +1,30 @@ @page "/" +@using System.Security.Claims +@inject AuthenticationStateProvider AuthenticationStateProvider Index

Hello, world!

-Welcome to your new app. +@if (User != null && User.Identity.IsAuthenticated) +{ +

Welcome, @User.Identity.Name!

+} +else +{ +

Welcome to our app!

+

You are not authenticated.

+

Please log in to continue.

+} - \ No newline at end of file + + +@code { + private ClaimsPrincipal User { get; set; } + + protected override async Task OnInitializedAsync() + { + var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync(); + User = authState.User; + } +} \ No newline at end of file diff --git a/samples/BlazorServer/Pages/_Host.cshtml b/samples/BlazorServer/Pages/_Host.cshtml deleted file mode 100644 index ceddf59..0000000 --- a/samples/BlazorServer/Pages/_Host.cshtml +++ /dev/null @@ -1,8 +0,0 @@ -@page "/" -@namespace BlazorServer.Pages -@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers -@{ - Layout = "_Layout"; -} - - \ No newline at end of file diff --git a/samples/BlazorServer/Pages/_Layout.cshtml b/samples/BlazorServer/Pages/_Layout.cshtml deleted file mode 100644 index 815cd75..0000000 --- a/samples/BlazorServer/Pages/_Layout.cshtml +++ /dev/null @@ -1,32 +0,0 @@ -@using Microsoft.AspNetCore.Components.Web -@namespace BlazorServer.Pages -@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers - - - - - - - - - - - - - -@RenderBody() - -
- - An error has occurred. This application may no longer respond until reloaded. - - - An unhandled exception has occurred. See browser dev tools for details. - - Reload - 🗙 -
- - - - \ No newline at end of file diff --git a/samples/BlazorServer/Routes.razor b/samples/BlazorServer/Routes.razor new file mode 100644 index 0000000..c519d1b --- /dev/null +++ b/samples/BlazorServer/Routes.razor @@ -0,0 +1,25 @@ + + + + + + @if (context.User.Identity?.IsAuthenticated != true) + { + + } + else + { +

You are not authorized to access this resource.

+ } +
+
+ +
+ + Not found + +

Sorry, there's nothing at this address.

+
+
+
+
\ No newline at end of file diff --git a/samples/BlazorServer/_Imports.razor b/samples/BlazorServer/_Imports.razor index 84b0efa..74a8047 100644 --- a/samples/BlazorServer/_Imports.razor +++ b/samples/BlazorServer/_Imports.razor @@ -7,4 +7,5 @@ @using Microsoft.AspNetCore.Components.Web.Virtualization @using Microsoft.JSInterop @using BlazorServer -@using BlazorServer.Shared \ No newline at end of file +@using BlazorServer.Shared +@using static Microsoft.AspNetCore.Components.Web.RenderMode \ No newline at end of file