Skip to content

Commit

Permalink
Merge pull request #105 from maxmantz/blazor-web-app
Browse files Browse the repository at this point in the history
migrated BlazorServer to Blazor web app
  • Loading branch information
damianh authored Oct 4, 2024
2 parents 1d8f8ab + 77d81c3 commit 29bef25
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 71 deletions.
61 changes: 36 additions & 25 deletions samples/BlazorServer/App.razor
Original file line number Diff line number Diff line change
@@ -1,25 +1,36 @@
<CascadingAuthenticationState>
<Router AppAssembly="@typeof(App).Assembly">
<Found Context="routeData">
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)">
<NotAuthorized>
@if (context.User.Identity?.IsAuthenticated != true)
{
<RedirectToLogin />
}
else
{
<p role="alert">You are not authorized to access this resource.</p>
}
</NotAuthorized>
</AuthorizeRouteView>
<FocusOnNavigate RouteData="@routeData" Selector="h1"/>
</Found>
<NotFound>
<PageTitle>Not found</PageTitle>
<LayoutView Layout="@typeof(MainLayout)">
<p role="alert">Sorry, there's nothing at this address.</p>
</LayoutView>
</NotFound>
</Router>
</CascadingAuthenticationState>
@inject IHostEnvironment Env

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<base href="/" />
<link rel="stylesheet" href="css/bootstrap/bootstrap.min.css" />
<link href="css/site.css" rel="stylesheet" />
<link href="BlazorServer.styles.css" rel="stylesheet" />
<HeadOutlet @rendermode="InteractiveServer" />
</head>
<body>
<Routes @rendermode="InteractiveServer" />
<div id="blazor-error-ui">
@if (Env.IsDevelopment())
{
<text>
An unhandled exception has occurred. See browser dev tools for details.
</text>
}
else
{
<text>
An error has occurred. This app may no longer respond until reloaded.
</text>
}
<a href="" class="reload">Reload</a>
<a class="dismiss">🗙</a>
</div>

<script src="_framework/blazor.web.js"></script>
</body>
</html>

9 changes: 6 additions & 3 deletions samples/BlazorServer/HostingExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using BlazorServer.Plumbing;
using BlazorServer.Services;
using Serilog;
using BlazorServer;

namespace BlazorServer;

Expand Down Expand Up @@ -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<WeatherForecastService>();

Expand All @@ -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<App>()
.AddInteractiveServerRenderMode();

return app;
}
Expand Down
25 changes: 23 additions & 2 deletions samples/BlazorServer/Pages/Index.razor
Original file line number Diff line number Diff line change
@@ -1,9 +1,30 @@
@page "/"
@using System.Security.Claims
@inject AuthenticationStateProvider AuthenticationStateProvider

<PageTitle>Index</PageTitle>

<h1>Hello, world!</h1>

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

Check warning on line 9 in samples/BlazorServer/Pages/Index.razor

View workflow job for this annotation

GitHub Actions / macOS-latest

Dereference of a possibly null reference.

Check warning on line 9 in samples/BlazorServer/Pages/Index.razor

View workflow job for this annotation

GitHub Actions / macOS-latest

Dereference of a possibly null reference.

Check warning on line 9 in samples/BlazorServer/Pages/Index.razor

View workflow job for this annotation

GitHub Actions / ubuntu-latest

Dereference of a possibly null reference.

Check warning on line 9 in samples/BlazorServer/Pages/Index.razor

View workflow job for this annotation

GitHub Actions / ubuntu-latest

Dereference of a possibly null reference.

Check warning on line 9 in samples/BlazorServer/Pages/Index.razor

View workflow job for this annotation

GitHub Actions / windows-latest

Dereference of a possibly null reference.

Check warning on line 9 in samples/BlazorServer/Pages/Index.razor

View workflow job for this annotation

GitHub Actions / windows-latest

Dereference of a possibly null reference.
{
<p>Welcome, @User.Identity.Name!</p>
}
else
{
<p>Welcome to our app!</p>
<p>You are not authenticated.</p>
<p>Please <NavLink href="/account/login">log in</NavLink> to continue.</p>
}

<SurveyPrompt Title="How is Blazor working for you?"/>
<SurveyPrompt Title="How is Blazor working for you?"/>

@code {
private ClaimsPrincipal User { get; set; }

Check warning on line 23 in samples/BlazorServer/Pages/Index.razor

View workflow job for this annotation

GitHub Actions / macOS-latest

Non-nullable property 'User' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 23 in samples/BlazorServer/Pages/Index.razor

View workflow job for this annotation

GitHub Actions / macOS-latest

Non-nullable property 'User' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 23 in samples/BlazorServer/Pages/Index.razor

View workflow job for this annotation

GitHub Actions / ubuntu-latest

Non-nullable property 'User' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 23 in samples/BlazorServer/Pages/Index.razor

View workflow job for this annotation

GitHub Actions / ubuntu-latest

Non-nullable property 'User' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 23 in samples/BlazorServer/Pages/Index.razor

View workflow job for this annotation

GitHub Actions / windows-latest

Non-nullable property 'User' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 23 in samples/BlazorServer/Pages/Index.razor

View workflow job for this annotation

GitHub Actions / windows-latest

Non-nullable property 'User' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

protected override async Task OnInitializedAsync()
{
var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
User = authState.User;
}
}
8 changes: 0 additions & 8 deletions samples/BlazorServer/Pages/_Host.cshtml

This file was deleted.

32 changes: 0 additions & 32 deletions samples/BlazorServer/Pages/_Layout.cshtml

This file was deleted.

25 changes: 25 additions & 0 deletions samples/BlazorServer/Routes.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<CascadingAuthenticationState>
<Router AppAssembly="@typeof(App).Assembly">
<Found Context="routeData">
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)">
<NotAuthorized>
@if (context.User.Identity?.IsAuthenticated != true)
{
<RedirectToLogin />
}
else
{
<p role="alert">You are not authorized to access this resource.</p>
}
</NotAuthorized>
</AuthorizeRouteView>
<FocusOnNavigate RouteData="@routeData" Selector="h1" />
</Found>
<NotFound>
<PageTitle>Not found</PageTitle>
<LayoutView Layout="@typeof(MainLayout)">
<p role="alert">Sorry, there's nothing at this address.</p>
</LayoutView>
</NotFound>
</Router>
</CascadingAuthenticationState>
3 changes: 2 additions & 1 deletion samples/BlazorServer/_Imports.razor
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
@using Microsoft.AspNetCore.Components.Web.Virtualization
@using Microsoft.JSInterop
@using BlazorServer
@using BlazorServer.Shared
@using BlazorServer.Shared
@using static Microsoft.AspNetCore.Components.Web.RenderMode

0 comments on commit 29bef25

Please sign in to comment.