3

I have a Blazor server-side project which I've been developing in Visual Studio 2019, using .NET5. All has been working fine.

I've just deployed the site to a test server (which has two such sites already running on it, so I know the server has everything it needs to run the site), but none of the Blazor stuff seems to work. Specifically...

  • Clicking on a button doesn't do anything
  • Clicking on a link refreshes the entire page, instead of just rendering the new HTML
  • Authorisation seems to be ignored, so a page with an [Authorize] attribute is visible to anyone

Don't know what code to show, as it all works when running in VS. Here is the contents of App.razor...

<CascadingAuthenticationState>
  <Router AppAssembly="@typeof(Program).Assembly">
    <Found Context="routeData">
      <AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)">
        <NotAuthorized>
          <RedirectToLogin />
        </NotAuthorized>
      </AuthorizeRouteView>
    </Found>
    <NotFound>
      <AuthorizeView>
        <Authorized>
          <LayoutView Layout="@typeof(MainLayout)">
            <NotFound />
          </LayoutView>
        </Authorized>
        <NotAuthorized>
          <RedirectToLogin />
        </NotAuthorized>
      </AuthorizeView>
    </NotFound>
  </Router>
</CascadingAuthenticationState>

The _Imports.razor file contains all the right usings, specifically including...

@using Microsoft.AspNetCore.Authorization
@using Microsoft.AspNetCore.Components.Authorization
@using Microsoft.AspNetCore.Components.Forms
@using Microsoft.AspNetCore.Components.Routing
@using Microsoft.AspNetCore.Components.Web

Pages that are not supposed to be accessible to non-logged-in users are decorated with [Authorize(Roles = "Admin")] in the code-behind or @attribute [Authorize(Roles = "Admin")] in the .razor file.

Anyone any ideas what I could have done wrong? Please advise if there is any more information I can give. Thanks

Update I just tried deploying the site to another domain on the same server, and it runs fine. I also tried pointing the other site to the folder for this one, and it worked fine (so it's not the deployed files), and then pointing this site to the other's folder, and it didn't work. So, it looks like there is something wrong with the site itself, but I ahve no idea what as it looks like the other.

7
  • 1
    Go check the Event Viewer on the Server if you're running on a Windows Server. Or similar for other operating systems. This should give you a bigger picture of the issue. You should be getting errors.
    – Marius
    Commented Feb 22, 2021 at 18:49
  • @Marius No, nothing in the event viewer at all related to this site. Any other ideas? Thanks Commented Feb 22, 2021 at 21:05
  • What's up, Avrohom ? Your code have some issues, probably not related to the main issue you're seemingly facing. I guess you first need to be sure that now your app is working fine, before you deal with petty things.
    – enet
    Commented Feb 22, 2021 at 23:27
  • @enet Hello. Nope, the code itself it working fine. Please see the update to my question. Commented Feb 23, 2021 at 13:58
  • Thanks for following up, @AvrohomYisroel. Please move your solution from the top of your question into a new answer below. Self-answers are encouraged! Also, feel free to accept your own answer.
    – Kirk Woll
    Commented Feb 23, 2021 at 20:11

2 Answers 2

11

It turned out that the problem was nothing to do with my code, nor my site settings on the server. I use Cloudflare, and had set it to minify HTML. This removes the two HTML comments that are essential for Blazor to work. See this article for more information.

So, if you are using Cloudflare, by all means set it to minify JavaScript and CSS, but DO not minify HTML.

5
  • 1
    You are a legend. Commented May 21, 2021 at 11:33
  • @KyleWhittington Hee hee, if only! All we have to do now is persuade Microsoft to find a better way of including the Blazor data in the page. Commented May 21, 2021 at 14:58
  • 1
    Ya saved the day Commented Oct 16, 2022 at 16:49
  • How did you even figure this out ?? Commented Feb 13, 2023 at 11:40
  • See the article I linked in my answer. We found that after a lot of searching Commented Feb 13, 2023 at 15:02
0

If you're hosting your application on IIS and your Blazor is a Server Side project, make sure the WebSockets feature is enabled in the Add Roles and Features wizard by following this instructions.

Hope this helps someone

For reference => https://learn.microsoft.com/en-us/aspnet/core/blazor/host-and-deploy/server?view=aspnetcore-8.0#iis

Not the answer you're looking for? Browse other questions tagged or ask your own question.