Skip to content

Improve Blazor project template #52234

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unlike the server template, this does not have an accessibility modifier. Neither does the Main method. I would have made both the class and public like we do for the server project, but this made the @typeof(Program).Assembly reference in Routes.razor ambiguous.

<Router AppAssembly="@typeof(Program).Assembly" AdditionalAssemblies="new[] { typeof(Client._Imports).Assembly }">

I could change that to be @typeof(BlazorWeb_CSharp.Program).Assembly instead and make this public. And then only qualify it when you've disabled top level statements.

Does anyone prefer that, or is everyone fine the way it is?

{
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