Skip to content

Conversation

@ilonatommy
Copy link
Member

@ilonatommy ilonatommy commented Jun 17, 2025

This PR is updating Blazor CRUD scaffolder to use NavigationManager.NotFound() instead of NavigationManager.NavigateTo("notfound"). I am not sure how to apply all the necessary changes. I will list all the actions needed and would be grateful for help in completing it, cc @deepchoudhery, @tlmii.

Changes:

Fixes #3168

Copy link
Member

@javiercn javiercn left a comment

Choose a reason for hiding this comment

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

Should we have the scaffolder create the NotFound page if it doesn't exist? Similarly, add the required code (UseStatusCodePages and NotFoundPageType)

@danroth27
Copy link
Member

Note that these changes are specific to .NET 10. For .NET 9 and earlier we'll need to continue to generate the existing code for handling not found responses.

deepchoudhery

This comment was marked as outdated.

@deepchoudhery deepchoudhery self-requested a review June 23, 2025 18:05
@deepchoudhery deepchoudhery dismissed their stale review June 23, 2025 18:28

premature

@danroth27
Copy link
Member

Should we have the scaffolder create the NotFound page if it doesn't exist? Similarly, add the required code (UseStatusCodePages and NotFoundPageType)

The three required additional changes to do that are:

  1. Add the /Components/Pages/NotFound.razor file, copied from the Blazor Web App template
  2. Update the Router in Routes.razor to add the NotFoundPage="typeof(Pages.NotFound)" parameter
<Router AppAssembly="typeof(Program).Assembly" NotFoundPage="typeof(Pages.NotFound)">
    <Found Context="routeData">
        <RouteView RouteData="routeData" DefaultLayout="typeof(Layout.MainLayout)" />
        <FocusOnNavigate RouteData="routeData" Selector="h1" />
    </Found>
</Router>
  1. Add the status code pages middleware in Program.cs:
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Error", createScopeForErrors: true);
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}
app.UseStatusCodePagesWithReExecute("/not-found", createScopeForErrors: true); // ADD HERE

app.UseHttpsRedirection();

@phenning
Copy link
Member

phenning commented Aug 1, 2025

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@phenning phenning self-assigned this Aug 1, 2025
@javiercn
Copy link
Member

javiercn commented Aug 6, 2025

FYI,

@phenning Are you actively working on this?

I didn't see it and I started copiloting a different PR with more fixes here #3178. Maybe we can continue with that one?

@danroth27
Copy link
Member

danroth27 commented Aug 27, 2025

@javiercn It looks like you had to abandon #3178, so I assume this PR is still where we should focus our attention, correct? Are there additional changes we need to account for in this PR beyond what is already listed above?

@ilonatommy
Copy link
Member Author

@bleaphar, are you able to test the recent changes and confirm if they are correct?

@bleaphar
Copy link
Contributor

bleaphar commented Sep 9, 2025

@bleaphar, are you able to test the recent changes and confirm if they are correct?

I was just able to test and verify that these changes are correct and scaffolding is working as intended. Here's a link to a sample repo I created: https://github.com/bleaphar/BlazorWebAppCRUD.git. If you examine the commit history, you can find the diff between how the template comes and post using these proposed changes.

@ilonatommy
Copy link
Member Author

ilonatommy commented Sep 10, 2025

@bleaphar, thank you!

Based on the Post Blazor CRUD Scaffolding commit we can see that the changes did not get applied correctly. We have e.g.
https://github.com/bleaphar/BlazorWebAppCRUD/blob/132a1899310e61d93f0071a315bb64b022d9dcf8/BlazorWebAppCRUD/BlazorWebAppCRUD/Components/Pages/MoviePages/Details.razor#L47

I have one concern about the testing methodology. You probably did it on a non-edited template. These templates already contain some of the changes that we want scaffolder to apply. We can see, examining the "Post Blazor CRUD Scaffolding" commit, that indeed, my changes did not break any stuff but at the same time, none of the editions this PR is introducing was applied.
We have to:

And then observe how they are getting added back.

However, based on the current test, we already can see that the lines NavigationManager.NavigateTo(\"notfound\") were not replaced by NavigationManager.NotFound(). How is it possible?

@danroth27
Copy link
Member

Or just test on a Blazor Web App originally created with .NET 9 and then retargeted to .NET 10.

@danroth27
Copy link
Member

Note that we also need to version these scaffolding updates so that the .NET 9 version of the scaffolder is preserved. This is different from the old scaffolding system where each scaffolder version shipped as a separate package from a separate branch. With dotnet scaffold it seems that all supported versions of the scaffolder need to ship together so that you can scaffold for both .NET 9 & 10 based projects.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Update the Blazor CRUD scaffolder for .NET 10 to use NavigationManager.NotFound()

6 participants