How Redirect To Not Found Without Change Url In Blazor .Net 8

Mahdi Elahi 31 Reputation points
2024-01-14T07:12:16.04+00:00

Hi,
How to redirect user to not found when page is not exists without change url in blazor ? Like this link : git not found

Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,571 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Mahdi Elahi 31 Reputation points
    2024-01-16T19:27:08.6133333+00:00

    @Farid Uddin Kiron MSFT No I dont have any error
    but i want load not found component content without navigation to not found page and without change url
    like this : example for not found page
    its's not change url just load content
    not found page

    0 comments No comments

  2. Bruce (SqlWork.com) 64,826 Reputation points
    2024-01-16T23:56:22.6433333+00:00

    just follow the docs and a not found component to the <Router>

    <Router ...>
        ...
        <NotFound>
            ...
        </NotFound>
    </Router>
    

    see docs:

    https://learn.microsoft.com/en-us/aspnet/core/blazor/fundamentals/routing?view=aspnetcore-8.0


  3. S Luiten 0 Reputation points
    2024-09-26T14:06:41.75+00:00

    The solution is to use NotFound in combination with UseStatusCodePagesWithReExecute:

    Step 1: Add the following to Program.cs

    app.UseHttpsRedirection();
    
    // Add this line
    app.UseStatusCodePagesWithReExecute("/not-found");
    
    app.UseStaticFiles();
    
    ...
    

    Step 2: Add a dummy .razor page with the same url

    @page "/not-found"
    
    <My404Component />
    

    Step 3: Add a NotFound renderfragment to Routes.razor

    <Router>
      <Found>
        ...
      </Found>
      <NotFound>
        <LayoutView Layout="typeof(MyLayout)">
          <My404Component />
        </LayoutView>
      </NotFound>
    </Router>
    

    The documentation states NotFound in Router is no longer supported, but this is actually not true, it still works but only if you do step 1 and 2 first, see https://github.com/dotnet/aspnetcore/issues/51203#issuecomment-1992085307

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.