Skip to content

Commit

Permalink
Improve Blazor project template (#52234)
Browse files Browse the repository at this point in the history
# Use consistent code style in Blazor project templates

- Removed usage of top-level statements in client project if "Do not use top-level statements" is selected
- Removed extra "@" in `@render-mode` values
- Always use `typeof(Namespace._Imports).Assembly` instead of `typeof(Counter).Assembly` so the compilation does not break when the sample Counter component is removed, and so the code is more consistent with how it is when no sample content is generated
- Added Account/AccessDenied endpoint to individual auth option to match Identity UI razor pages. This is shown when the user is authenticated but unauthorized by default.

Fixes #52079
Fixes #52084
Fixes #52167

## Customer Impact

In addition to not using top-level statements when the customer requests that we don't, this improves code style consistency within the Blazor project template and with the Blazor docs.

## Regression?

- [ ] Yes
- [x] No

## Risk

- [ ] High
- [ ] Medium
- [x] Low

These are small stylistic changes to the Blazor project templates.

## Verification

- [x] Manual (required)
- [ ] Automated

## Packaging changes reviewed?

- [ ] Yes
- [ ] No
- [x] N/A
  • Loading branch information
halter73 committed Nov 28, 2023
1 parent 4373734 commit c909955
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,15 @@
{
"condition": "(!UseProgramMain)",
"exclude": [
"BlazorWeb-CSharp/Program.Main.cs"
"BlazorWeb-CSharp/Program.Main.cs",
"BlazorWeb-CSharp.Client/Program.Main.cs"
]
},
{
"condition": "(UseProgramMain)",
"exclude": [
"BlazorWeb-CSharp/Program.cs"
"BlazorWeb-CSharp/Program.cs",
"BlazorWeb-CSharp.Client/Program.cs"
],
"rename": {
"Program.Main.cs": "Program.cs"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#if (IndividualLocalAuth)
using BlazorWeb_CSharp.Client;
using Microsoft.AspNetCore.Components.Authorization;
#endif
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;

namespace BlazorWeb_CSharp.Client;

class Program
{
static async Task Main(string[] args)
{
var builder = WebAssemblyHostBuilder.CreateDefault(args);

#if (IndividualLocalAuth)
builder.Services.AddAuthorizationCore();
builder.Services.AddCascadingAuthenticationState();
builder.Services.AddSingleton<AuthenticationStateProvider, PersistentAuthenticationStateProvider>();

#endif
await builder.Build().RunAsync();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@page "/Account/AccessDenied"

<PageTitle>Access denied</PageTitle>

<header>
<h1 class="text-danger">Access denied</h1>
<p class="text-danger">You do not have access to this resource.</p>
</header>
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,27 @@
@*#if (!InteractiveAtRoot)
<HeadOutlet />
##elseif (IndividualLocalAuth)
<HeadOutlet @rendermode="@RenderModeForPage" />
<HeadOutlet @rendermode="RenderModeForPage" />
##elseif (UseServer && UseWebAssembly)
<HeadOutlet @rendermode="@InteractiveAuto" />
<HeadOutlet @rendermode="InteractiveAuto" />
##elseif (UseServer)
<HeadOutlet @rendermode="@InteractiveServer" />
<HeadOutlet @rendermode="InteractiveServer" />
##else
<HeadOutlet @rendermode="@InteractiveWebAssembly" />
<HeadOutlet @rendermode="InteractiveWebAssembly" />
##endif*@
</head>

<body>
@*#if (!InteractiveAtRoot)
<Routes />
##elseif (IndividualLocalAuth)
<Routes @rendermode="@RenderModeForPage" />
<Routes @rendermode="RenderModeForPage" />
##elseif (UseServer && UseWebAssembly)
<Routes @rendermode="@InteractiveAuto" />
<Routes @rendermode="InteractiveAuto" />
##elseif (UseServer)
<Routes @rendermode="@InteractiveServer" />
<Routes @rendermode="InteractiveServer" />
##else
<Routes @rendermode="@InteractiveWebAssembly" />
<Routes @rendermode="InteractiveWebAssembly" />
##endif*@
<script src="_framework/blazor.web.js"></script>
</body>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@
@using BlazorWeb_CSharp.Components.Account.Shared
##endif*@
@*#if (UseWebAssembly && !InteractiveAtRoot)
<Router AppAssembly="@typeof(Program).Assembly" AdditionalAssemblies="new[] { typeof(Client._Imports).Assembly }">
<Router AppAssembly="typeof(Program).Assembly" AdditionalAssemblies="new[] { typeof(Client._Imports).Assembly }">
##else
<Router AppAssembly="@typeof(Program).Assembly">
<Router AppAssembly="typeof(Program).Assembly">
##endif*@
<Found Context="routeData">
@*#if (IndividualLocalAuth)
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(Layout.MainLayout)">
<AuthorizeRouteView RouteData="routeData" DefaultLayout="typeof(Layout.MainLayout)">
<NotAuthorized>
<RedirectToLogin />
</NotAuthorized>
</AuthorizeRouteView>
##else
<RouteView RouteData="@routeData" DefaultLayout="@typeof(Layout.MainLayout)" />
<RouteView RouteData="routeData" DefaultLayout="typeof(Layout.MainLayout)" />
##endif*@
<FocusOnNavigate RouteData="@routeData" Selector="h1" />
<FocusOnNavigate RouteData="routeData" Selector="h1" />
</Found>
</Router>
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,7 @@ public static void Main(string[] args)
#else
app.MapRazorComponents<App>();
#endif
#if (UseWebAssembly && SampleContent)
.AddAdditionalAssemblies(typeof(Counter).Assembly);
#elif (UseWebAssembly)
#if (UseWebAssembly)
.AddAdditionalAssemblies(typeof(Client._Imports).Assembly);
#endif

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,7 @@
#else
app.MapRazorComponents<App>();
#endif
#if (UseWebAssembly && SampleContent)
.AddAdditionalAssemblies(typeof(Counter).Assembly);
#elif (UseWebAssembly)
#if (UseWebAssembly)
.AddAdditionalAssemblies(typeof(BlazorWeb_CSharp.Client._Imports).Assembly);
#endif

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,7 @@
"Components/Account/IdentityNoOpEmailSender.cs",
"Components/Account/IdentityRedirectManager.cs",
"Components/Account/IdentityUserAccessor.cs",
"Components/Account/Pages/AccessDenied.razor",
"Components/Account/Pages/ConfirmEmail.razor",
"Components/Account/Pages/ConfirmEmailChange.razor",
"Components/Account/Pages/ExternalLogin.razor",
Expand Down Expand Up @@ -642,6 +643,7 @@
"Components/Account/IdentityRedirectManager.cs",
"Components/Account/IdentityRevalidatingAuthenticationStateProvider.cs",
"Components/Account/IdentityUserAccessor.cs",
"Components/Account/Pages/AccessDenied.razor",
"Components/Account/Pages/ConfirmEmail.razor",
"Components/Account/Pages/ConfirmEmailChange.razor",
"Components/Account/Pages/ExternalLogin.razor",
Expand Down Expand Up @@ -718,6 +720,7 @@
"Components/Account/IdentityRedirectManager.cs",
"Components/Account/IdentityRevalidatingAuthenticationStateProvider.cs",
"Components/Account/IdentityUserAccessor.cs",
"Components/Account/Pages/AccessDenied.razor",
"Components/Account/Pages/ConfirmEmail.razor",
"Components/Account/Pages/ConfirmEmailChange.razor",
"Components/Account/Pages/ExternalLogin.razor",
Expand Down Expand Up @@ -836,6 +839,7 @@
"{ProjectName}/Components/Account/IdentityNoOpEmailSender.cs",
"{ProjectName}/Components/Account/IdentityRedirectManager.cs",
"{ProjectName}/Components/Account/IdentityUserAccessor.cs",
"{ProjectName}/Components/Account/Pages/AccessDenied.razor",
"{ProjectName}/Components/Account/Pages/ConfirmEmail.razor",
"{ProjectName}/Components/Account/Pages/ConfirmEmailChange.razor",
"{ProjectName}/Components/Account/Pages/ExternalLogin.razor",
Expand Down Expand Up @@ -953,6 +957,7 @@
"{ProjectName}/Components/Account/IdentityNoOpEmailSender.cs",
"{ProjectName}/Components/Account/IdentityRedirectManager.cs",
"{ProjectName}/Components/Account/IdentityUserAccessor.cs",
"{ProjectName}/Components/Account/Pages/AccessDenied.razor",
"{ProjectName}/Components/Account/Pages/ConfirmEmail.razor",
"{ProjectName}/Components/Account/Pages/ConfirmEmailChange.razor",
"{ProjectName}/Components/Account/Pages/ExternalLogin.razor",
Expand Down Expand Up @@ -1214,6 +1219,7 @@
"{ProjectName}/Components/Account/IdentityNoOpEmailSender.cs",
"{ProjectName}/Components/Account/IdentityRedirectManager.cs",
"{ProjectName}/Components/Account/IdentityUserAccessor.cs",
"{ProjectName}/Components/Account/Pages/AccessDenied.razor",
"{ProjectName}/Components/Account/Pages/ConfirmEmail.razor",
"{ProjectName}/Components/Account/Pages/ConfirmEmailChange.razor",
"{ProjectName}/Components/Account/Pages/ExternalLogin.razor",
Expand Down Expand Up @@ -1282,6 +1288,7 @@
"Components/Account/IdentityRedirectManager.cs",
"Components/Account/IdentityRevalidatingAuthenticationStateProvider.cs",
"Components/Account/IdentityUserAccessor.cs",
"Components/Account/Pages/AccessDenied.razor",
"Components/Account/Pages/ConfirmEmail.razor",
"Components/Account/Pages/ConfirmEmailChange.razor",
"Components/Account/Pages/ExternalLogin.razor",
Expand Down Expand Up @@ -1375,6 +1382,7 @@
"{ProjectName}/Components/Account/IdentityNoOpEmailSender.cs",
"{ProjectName}/Components/Account/IdentityRedirectManager.cs",
"{ProjectName}/Components/Account/IdentityUserAccessor.cs",
"{ProjectName}/Components/Account/Pages/AccessDenied.razor",
"{ProjectName}/Components/Account/Pages/ConfirmEmail.razor",
"{ProjectName}/Components/Account/Pages/ConfirmEmailChange.razor",
"{ProjectName}/Components/Account/Pages/ExternalLogin.razor",
Expand Down Expand Up @@ -1459,6 +1467,7 @@
"{ProjectName}/Components/Account/IdentityNoOpEmailSender.cs",
"{ProjectName}/Components/Account/IdentityRedirectManager.cs",
"{ProjectName}/Components/Account/IdentityUserAccessor.cs",
"{ProjectName}/Components/Account/Pages/AccessDenied.razor",
"{ProjectName}/Components/Account/Pages/ConfirmEmail.razor",
"{ProjectName}/Components/Account/Pages/ConfirmEmailChange.razor",
"{ProjectName}/Components/Account/Pages/ExternalLogin.razor",
Expand Down

0 comments on commit c909955

Please sign in to comment.