GithubHelp home page GithubHelp logo

datvm / demoblazorinsideweb Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 1 MB

This is a demo to add Blazor Assembly inside an existing ASP.NET Core website. Here Blazor simply acts as a Web App content inside a Web Page.

HTML 50.56% C# 15.79% CSS 32.18% JavaScript 1.47%

demoblazorinsideweb's Introduction

Introduction

This repo is to solve my problem with the the following scenario:

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

<div id="app">
    Blazor app here
</div>

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

The final result looks like this:

Demo UI

You can see the steps in this repo:

  • At commit #2be15e3, I have an ASP.NET Core website (just a starting template). Consider this your existing website.

  • Commit #ba13eaf to 8bdae3a adds a Blazor app to the website. You can see the changes in the commit.

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" />
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.