1

It's similar to this question: Include a Blazor Webassembly project into an existing ASP.NET Core project but my scenario is different:

<!-- Other content: header, texts, etc -->

<app>
    Blazor app here
</app>

<!-- Other content: more text, footer, scripts, etc -->

The Blazor app does not need to know about the URL, I will just use it for nice Component features, similar to Web Component.

Can I achieve this? If possible, without a separte Blazor WebAssembly project but it's okay if I have to. Also would be great if I can host more than one app (at https://www.example.com/feature1 and https://www.example.com/feature2) on a website.

Luke Vo
  • 17,859
  • 21
  • 105
  • 181
  • Why do you want to load single pages in Web Assembly? You have to load WASM everytine. Slow. Why not use Server? The page just loads the page component, missing out the router. Can show you an example if you wish. – MrC aka Shaun Curtis Mar 09 '23 at 17:18
  • @MrCakaShaunCurtis SEO is one come to mind. I know there is server-rendering but that also doesn't justify moving everything to Blazor Server when your ASP.NET Core website is already stable. Also IMO even if I start anew, I wouldn't use Blazor Server because it uses a lot of server's resources (keeping RTC connection alive, ...) while serving HTML is perfectly fine. – Luke Vo Mar 11 '23 at 10:02
  • Agreed, but the problem with loading each page as a WASM SPA is speed. MS is working on speeding things up, but I'm not sure how fast they can make that initial page load. If you're happy with the load speed then no problems. – MrC aka Shaun Curtis Mar 11 '23 at 11:02
  • @MrCakaShaunCurtis well the good thing about this is you can wrap the app with your own content (provided by standard HTML through ASP.NET). So you can have proper content and even good loading screen while Blazor Assemblies are being loaded. I still see this as an absolute win. – Luke Vo Mar 11 '23 at 11:58

1 Answers1

1

I have successfully add a Blazor WebAssembly app to an existing ASP.NET Core app. You can check the demo repository here on GitHub. I also wrote the detailed step-by-step instruction there.

enter image description here

Step-by-step guide

1. Setup a new Blazor WebAssembly App (B)

  • Create a new Blazor WebAssembly App project, for example DemoBlazorInsideWeb.BlazorApp.

  • Update your router in App.razor to show the Index page for all routes:

<Router AppAssembly="@typeof(App).Assembly">
    <Found Context="routeData">
        <!-- Delete everything here -->
    </Found>
    <NotFound>
        <!-- Add this here -->
        <LayoutView Layout="@typeof(MainLayout)">
            <Index />
        </LayoutView>
    </NotFound>
</Router>
  • Optionally delete @page in your Index.razor file so no URL can ever be routed to it. This is to prevent a route accidentally match it.

  • Optionally delete index.html file in your wwwroot folder. However you may want to keep the content somewhere to copy its content later.

Note
You can still use Routing if you want to serve multiple apps on the same website (even though you have to pack all those apps inside this single project)

2. Setup your (existing) ASP.NET Core website (A)

  • Add the Blazor project as Reference to your ASP.NET Core website project (A refers to or depends on B).

  • Install Microsoft.AspNetCore.Components.WebAssembly.Server Nuget package:

dotnet add package Microsoft.AspNetCore.Components.WebAssembly.Server
  • In your app startup (Program.cs or Startup.cs), add app.UseBlazorFrameworkFiles() before app.UseStaticFiles():
app.UseBlazorFrameworkFiles();
app.UseStaticFiles();
  • In order to debug WASM app, you need to:

    • Add app.UseWebAssemblyDebugging(); in Development environment:

      if (!app.Environment.IsDevelopment())
      {
          // Usually the template has this block
      }
      else // And you add this block
      {
          app.UseWebAssemblyDebugging();
      }
      
    • Add inspectUri property to your launchSettings.json file (you should find it in Properties folder). Add it to whichever profile you use, https and/or IIS Express:

      {
          // ...
          "IIS Express": {
              // ...
              "inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}"
          }
      }
      

3. Setup the new web (Feature) page

In the ASP.NET Core Razor Page that you want to add the Blazor app, you need to setup Blazor's content

  • Add the HTML and Javascript content:
<!-- Other Razor code -->

<div id="app">
    <!-- Loading content before Blazor loads. You can copy it from index.html file -->
</div>

<div id="blazor-error-ui">
    An unhandled error has occurred.
    <a href="" class="reload">Reload</a>
    <a class="dismiss"></a>
</div>

<!-- Other Razor code -->

<!-- Add the script where relevant to your project -->
<script src="_framework/blazor.webassembly.js"></script>
  • You should also refer to the Blazor's CSS file, or simply move its content to your own ASP.NET Core project:
<!-- You need to have Heads section in your Layout -->
@section Heads {
    <link rel="stylesheet" href="~/css/app.css" />
}
Luke Vo
  • 17,859
  • 21
  • 105
  • 181