Blazor WASM & Azure Static Web Apps, routes not working

JohnM 25 Reputation points
2024-11-21T23:25:55.95+00:00

I have a blazor WASM static web app, the "/api/" & "/.auth/" urls that should be handled on the server side by Azure seem to get pick up by the blazor WASM routing.

authmenotfound the "sorry, theirs nothing..." here is coming from App.razor:

<CascadingAuthenticationState>
    <Router AppAssembly="@typeof(App).Assembly">
        <Found Context="routeData">
            <AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)">
                <Authorizing>
                    <p>Loading session state...</p>
                </Authorizing>
                <NotAuthorized>
                    <h1>Sorry</h1>
                    <p role="alert">You are not authorized to access this resource.</p>
                </NotAuthorized>
            </AuthorizeRouteView>
            <FocusOnNavigate RouteData="@routeData" Selector="h1" />
        </Found>
        <NotFound>
            <PageTitle>Not found</PageTitle>
            <LayoutView Layout="@typeof(MainLayout)">
                <p role="alert">Sorry, there's nothing at this address.</p>
            </LayoutView>
        </NotFound>
    </Router>
</CascadingAuthenticationState>


for example if enter https://xxxxx/.auth/me in the browser I get the following:

authrefresh

But if I CTRL+F5 refresh then I can get the expected response:

I've tried the following without any luck in the staticwebapp.config.json:

  "navigationFallback": {
    "rewrite": "/index.html",
    "exclude": [ "/api/*", "/.auth/*" ]
  },


The exact same thing happens with the api paths, if go to the following url, i get "Sorry, there's nothing at this address." if I CRTL+F5 I get the response from the function, if I then click the browsers refresh button back to "Sorry, there's nothing at this address.", only way to get correct response if via CTRL+F5

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,606 questions
Azure Static Web Apps
Azure Static Web Apps
An Azure service that provides streamlined full-stack web app development.
995 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 67,576 Reputation points
    2024-11-22T21:56:44.7366667+00:00

    that is correct. The Blazor WASM (or Server) will intercept browser navigation and use it as Blazor routing. In Blazor to navigate to an external URL, you use jsinterop and navigate via window.location.

    the azure functions are expected to be called via HttpClient. But if typed into a browser window not running Blazor, the server will handle (this is what f5 does - it unload Blazor and requests the /api url directly).


  2. JohnM 25 Reputation points
    2024-11-23T07:00:28.1133333+00:00

    This error came down to a invalid appsettings.json file, not that it stopped anything else from working that I noticed. Json doesn't support comments, hence file below was invalid.

    {
      "Logging": {
        "LogLevel": {
          "Default": "Information",
          "Microsoft.AspNetCore": "Warning"
        }
      }
      //"AzureAd": {
      //  "ClientId": "XXXXXXX-b093-4ddf-8b3d-6f653ccc1dc7",
      //  "Authority": "https://comusers.ciamlogin.com/comusers.onmicrosoft.com",
      //  //"Authority": "https://login.microsoftonline.com/XXXXXXXXX-e6a1-4f1b-baeb-604c7b67d2f2",
      //  "ValidateAuthority": true
      //}
    }
    
    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.