Mumrich.Templates 1.6.0-alpha.6

This is a prerelease version of Mumrich.Templates.
dotnet new install Mumrich.Templates::1.6.0-alpha.6
This package contains a .NET Template Package you can call from the shell/command line.

Mumrich.SpaDevMiddleware

A .NET Middleware for ASP.NET Core 6 that automatically integrates (multiple) Single-Page-Apps in a .NET-Webhost.

  • Automatic node-package install (npm/yarn/pnpm)
  • SPAs are automatically built uppon (dotnet publish triggers the build-script in package.json)
  • Automatic path-mapping for the SPA by aid of the supperb YARP
  • SPA Hot-Reloading supported
  • Custom-ENV-Variables can be passed to the Node-Instance via appsettings.json
  • Usage of MSBUILD-Variables supported

Usage

  1. Install the Mumrich.SpaDevMiddleware Nuget-Package into your Web-Project (.NET 6)

  2. Implement the ISpaDevServerSettings-interface:

    public class AppSettings : ISpaDevServerSettings
    {
        public Dictionary<string, SpaSettings> SinglePageApps { get; set; } = new();
    }
    
  3. Create the MSBuild-Item-Group in the csproj-File for node-package install and dotnet-publish to work "out of the box":

    <ItemGroup>
    
        <SpaRoot Include="app1\" />
        <SpaRoot Include="app2\" />
    </ItemGroup>
    
  4. Extend appsettings.json with a SPA-Config e.g.:

    {
      "Logging": {
        "LogLevel": {
          "Default": "Information",
          "Microsoft.AspNetCore": "Warning"
        }
      },
      "AllowedHosts": "*",
      "SinglePageApps": {
        "/": {
          "DevServerAddress": "http://localhost:3000/",
          "SpaRootPath": "app1",
          "NodePackageManager": "Yarn",
          "NodeStartScript": "dev",
          "Regex": "dev server running at:",
          "Bundler": "ViteJs",
          "Environment": {
            "HMR_PORT": "%ASPNETCORE_HTTPS_PORT%",
            "HMR_PROTOCOL": "wss"
          }
        },
        "/admin": {
          "DevServerAddress": "http://localhost:9000/",
          "SpaRootPath": "app2",
          "NodePackageManager": "Yarn",
          "NodeStartScript": "dev",
          "Regex": "Opening default browser",
          "Bundler": "ViteJs"
        }
      }
    }
    
  5. Register Services and setup app e.g.:

    using Mumrich.SpaDevMiddleware.Extensions;
    
    var builder = WebApplication.CreateBuilder(args);
    var appSettings = builder.Configuration.Get<AppSettings>();
    
    builder.Services.AddControllersWithViews();
    builder.Services.AddCors();
    builder.RegisterSinglePageAppDevMiddleware(appSettings);
    
    var app = builder.Build();
    
    app.UseStaticFiles();
    app.UseHttpsRedirection();
    app.UseRouting();
    app.UseCors();
    app.MapControllers();
    app.MapSinglePageApps(appSettings);
    
    app.Run();
    
  6. When using hot-reloading, adapt your SPA-bundling-setup accordingly to accept the .NET-Webhost-Proxy on the correct Port. Custom-ENV-Variables can be passed via appsettings.json - e.g for vite.config.ts:

    const protocol = process.env.HMR_PROTOCOL ?? "ws";
    const port = Number(process.env.HMR_PORT) ?? 3000;
    
    // https://vitejs.dev/config/
    export default defineConfig({
      plugins: [vue()],
      server: {
        port: 3000,
        strictPort: true,
        hmr: {
          protocol,
          port,
        },
      },
    });
    
  7. Run the app (dotnet run), navigate to the .NET-Webhost-Url and enjoy 👌!

Troubleshooting

  • See the working Example in the WebSpaVue-folder.

Credits

  • net6.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.6.0-alpha.6 95 10/5/2022
1.6.0-alpha.5 76 10/5/2022
1.6.0-alpha.4 90 10/5/2022
1.2.4 768 3/24/2023
1.2.3 210 3/23/2023
1.2.2 227 3/23/2023
1.2.2-PullRequest0013.16 75 3/23/2023
1.2.1 412 1/12/2023
1.2.1-PullRequest0012.21 92 1/12/2023