Skip to content
This repository was archived by the owner on Nov 19, 2024. It is now read-only.

Commit 29bef25

Browse files
authored
Merge pull request #105 from maxmantz/blazor-web-app
migrated BlazorServer to Blazor web app
2 parents 1d8f8ab + 77d81c3 commit 29bef25

File tree

7 files changed

+92
-71
lines changed

7 files changed

+92
-71
lines changed

samples/BlazorServer/App.razor

+36-25
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,36 @@
1-
<CascadingAuthenticationState>
2-
<Router AppAssembly="@typeof(App).Assembly">
3-
<Found Context="routeData">
4-
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)">
5-
<NotAuthorized>
6-
@if (context.User.Identity?.IsAuthenticated != true)
7-
{
8-
<RedirectToLogin />
9-
}
10-
else
11-
{
12-
<p role="alert">You are not authorized to access this resource.</p>
13-
}
14-
</NotAuthorized>
15-
</AuthorizeRouteView>
16-
<FocusOnNavigate RouteData="@routeData" Selector="h1"/>
17-
</Found>
18-
<NotFound>
19-
<PageTitle>Not found</PageTitle>
20-
<LayoutView Layout="@typeof(MainLayout)">
21-
<p role="alert">Sorry, there's nothing at this address.</p>
22-
</LayoutView>
23-
</NotFound>
24-
</Router>
25-
</CascadingAuthenticationState>
1+
@inject IHostEnvironment Env
2+
3+
<!DOCTYPE html>
4+
<html lang="en">
5+
<head>
6+
<meta charset="utf-8" />
7+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
8+
<base href="/" />
9+
<link rel="stylesheet" href="css/bootstrap/bootstrap.min.css" />
10+
<link href="css/site.css" rel="stylesheet" />
11+
<link href="BlazorServer.styles.css" rel="stylesheet" />
12+
<HeadOutlet @rendermode="InteractiveServer" />
13+
</head>
14+
<body>
15+
<Routes @rendermode="InteractiveServer" />
16+
<div id="blazor-error-ui">
17+
@if (Env.IsDevelopment())
18+
{
19+
<text>
20+
An unhandled exception has occurred. See browser dev tools for details.
21+
</text>
22+
}
23+
else
24+
{
25+
<text>
26+
An error has occurred. This app may no longer respond until reloaded.
27+
</text>
28+
}
29+
<a href="" class="reload">Reload</a>
30+
<a class="dismiss">🗙</a>
31+
</div>
32+
33+
<script src="_framework/blazor.web.js"></script>
34+
</body>
35+
</html>
36+

samples/BlazorServer/HostingExtensions.cs

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using BlazorServer.Plumbing;
22
using BlazorServer.Services;
33
using Serilog;
4+
using BlazorServer;
45

56
namespace BlazorServer;
67

@@ -65,7 +66,8 @@ public static WebApplication ConfigureServices(this WebApplicationBuilder builde
6566

6667
builder.Services.AddControllersWithViews();
6768
builder.Services.AddRazorPages();
68-
builder.Services.AddServerSideBlazor();
69+
builder.Services.AddRazorComponents()
70+
.AddInteractiveServerComponents();
6971

7072
builder.Services.AddSingleton<WeatherForecastService>();
7173

@@ -79,13 +81,14 @@ public static WebApplication ConfigurePipeline(this WebApplication app)
7981
app.UseStaticFiles();
8082

8183
app.UseRouting();
84+
app.UseAntiforgery();
8285

8386
app.UseAuthentication();
8487
app.UseAuthorization();
8588

8689
app.MapDefaultControllerRoute();
87-
app.MapBlazorHub();
88-
app.MapFallbackToPage("/_Host");
90+
app.MapRazorComponents<App>()
91+
.AddInteractiveServerRenderMode();
8992

9093
return app;
9194
}
+23-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,30 @@
11
@page "/"
2+
@using System.Security.Claims
3+
@inject AuthenticationStateProvider AuthenticationStateProvider
24

35
<PageTitle>Index</PageTitle>
46

57
<h1>Hello, world!</h1>
68

7-
Welcome to your new app.
9+
@if (User != null && User.Identity.IsAuthenticated)
10+
{
11+
<p>Welcome, @User.Identity.Name!</p>
12+
}
13+
else
14+
{
15+
<p>Welcome to our app!</p>
16+
<p>You are not authenticated.</p>
17+
<p>Please <NavLink href="/account/login">log in</NavLink> to continue.</p>
18+
}
819

9-
<SurveyPrompt Title="How is Blazor working for you?"/>
20+
<SurveyPrompt Title="How is Blazor working for you?"/>
21+
22+
@code {
23+
private ClaimsPrincipal User { get; set; }
24+
25+
protected override async Task OnInitializedAsync()
26+
{
27+
var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
28+
User = authState.User;
29+
}
30+
}

samples/BlazorServer/Pages/_Host.cshtml

-8
This file was deleted.

samples/BlazorServer/Pages/_Layout.cshtml

-32
This file was deleted.

samples/BlazorServer/Routes.razor

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<CascadingAuthenticationState>
2+
<Router AppAssembly="@typeof(App).Assembly">
3+
<Found Context="routeData">
4+
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)">
5+
<NotAuthorized>
6+
@if (context.User.Identity?.IsAuthenticated != true)
7+
{
8+
<RedirectToLogin />
9+
}
10+
else
11+
{
12+
<p role="alert">You are not authorized to access this resource.</p>
13+
}
14+
</NotAuthorized>
15+
</AuthorizeRouteView>
16+
<FocusOnNavigate RouteData="@routeData" Selector="h1" />
17+
</Found>
18+
<NotFound>
19+
<PageTitle>Not found</PageTitle>
20+
<LayoutView Layout="@typeof(MainLayout)">
21+
<p role="alert">Sorry, there's nothing at this address.</p>
22+
</LayoutView>
23+
</NotFound>
24+
</Router>
25+
</CascadingAuthenticationState>

samples/BlazorServer/_Imports.razor

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@
77
@using Microsoft.AspNetCore.Components.Web.Virtualization
88
@using Microsoft.JSInterop
99
@using BlazorServer
10-
@using BlazorServer.Shared
10+
@using BlazorServer.Shared
11+
@using static Microsoft.AspNetCore.Components.Web.RenderMode

0 commit comments

Comments
 (0)