Blazor refresh route 404 error when moved _Host

James Dearnley 20 Reputation points
2023-06-05T15:03:25.41+00:00

I have a new Blazor Server project, I set up some new pages and placed them in an Areas/ subfolder. I move _Host to Areas/General/Pages as well and so have to specify this in Program.cs via:
app.MapFallbackToPage("General", "/_Host");
The site seems to work fine. However, when I hit refresh I get a 404 error. If I move my _Host.cshtml file back into a root Pages/ folder and restore the default fallback it of course works. So my question is is it possible to move the _Host file without confusing routing. Or is there a way to specify I want to use a folder called Areas instead of Pages?

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,390 questions
0 comments No comments
{count} votes

Accepted answer
  1. Zhi Lv - MSFT 32,011 Reputation points Microsoft Vendor
    2023-06-06T02:52:04.24+00:00

    Hi @James Dearnley

    So my question is is it possible to move the _Host file without confusing routing. Or is there a way to specify I want to use a folder called Areas instead of Pages?

    We can move the _Host file to the Areas/subfolder. Then, when set up the root page of the app, we need to use the MapFallbackToAreaPage("{page}", "{Areas}") method, instead of the MapFallbackToPage method.

    Refer to the following sample: I create a MyTest Areas and add a _MyHost.cshtml host page:

    User's image

    And the _Host.cshtml page as below: you can also delete this _Host.cshtml page.

    User's image

    Then in the program, use the MapFallbackToAreaPage("{page}", "{Areas}") method to set up the root page of the app:

    User's image

    After that the navigation works well on my side.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,

    Dillion

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful