Multiple hosted ASP.NET Core Blazor WebAssembly apps: An unhandled error has occurred. Reload 🗙

Nasheayahu 40 Reputation points
2023-11-19T06:56:12.2266667+00:00

In completing "Multiple hosted ASP.NET Core Blazor WebAssembly apps successfully" successfully using the port host, I'm having trouble getting the Route subpath hosting working. I get this webpage and the browser debug console has this when navigating to https://localhost:5000/SecondApp/:

remmina_Kohan Development Workstation_10.30.60.16_20231119-063844

Larger view of brower errors:remmina_Kohan Development Workstation_10.30.60.16_20231119-065653

Server Program.cs:

using Microsoft.AspNetCore.ResponseCompression;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllersWithViews();
builder.Services.AddRazorPages();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseWebAssemblyDebugging();
}
else
{
    app.UseExceptionHandler("/Error");
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}

app.UseHttpsRedirection();

// if serving pages or views from the server app
//app.UseBlazorFrameworkFiles();
app.MapWhen(ctx => ctx.Request.Path.StartsWithSegments("/FirstApp",
	StringComparison.OrdinalIgnoreCase), first =>
	{
		first.UseBlazorFrameworkFiles("/FirstApp");
		first.UseStaticFiles();
		first.UseStaticFiles("/FirstApp");
		first.UseRouting();

		first.UseEndpoints(endpoints =>
		{
			endpoints.MapControllers();
			endpoints.MapFallbackToFile("/FirstApp/{*path:nonfile}",
				"FirstApp/index.html");
		});
	});

app.MapWhen(ctx => ctx.Request.Path.StartsWithSegments("/SecondApp",
	StringComparison.OrdinalIgnoreCase), second =>
	{
		second.UseBlazorFrameworkFiles("/SecondApp");
		second.UseStaticFiles();
		second.UseStaticFiles("/SecondApp");
		second.UseRouting();

		second.UseEndpoints(endpoints =>
		{
			endpoints.MapControllers();
			endpoints.MapFallbackToFile("/SecondApp/{*path:nonfile}",
				"SecondApp/index.html");
		});
	});

app.UseStaticFiles();

app.UseRouting();


app.MapRazorPages();
app.MapControllers();
// if serving pages or views from the server app
//app.MapFallbackToFile("index.html");

app.Run();

Client.csproj:

<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">

	<PropertyGroup>
		<TargetFramework>net7.0</TargetFramework>
		<Nullable>enable</Nullable>
		<ImplicitUsings>enable</ImplicitUsings>
		<StaticWebAssetBasePath>FirstApp</StaticWebAssetBasePath>
	</PropertyGroup>

	<ItemGroup>
		<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="7.0.13" />
		<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="7.0.13" PrivateAssets="all" />
	</ItemGroup>

	<ItemGroup>
		<ProjectReference Include="..\RazorClassLibrary1\RazorClassLibrary1.csproj" />
		<ProjectReference Include="..\Shared\BWAKohanyimApp.Shared.csproj" />
	</ItemGroup>

</Project>

SecondClient.csproj:

<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">

	<PropertyGroup>
		<TargetFramework>net7.0</TargetFramework>
		<Nullable>enable</Nullable>
		<ImplicitUsings>enable</ImplicitUsings>
		<StaticWebAssetBasePath>SecondApp</StaticWebAssetBasePath>
	</PropertyGroup>

	<ItemGroup>
		<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="7.0.13" />
		<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="7.0.13" PrivateAssets="all" />
	</ItemGroup>

	<ItemGroup>
		<ProjectReference Include="..\RazorClassLibrary1\RazorClassLibrary1.csproj" />
		<ProjectReference Include="..\Shared\BWAKohanyimApp.Shared.csproj" />
	</ItemGroup>

</Project>

Server project's Properties/launchSettings.json

{
    "iisSettings": {
      "windowsAuthentication": false,
      "anonymousAuthentication": true,
      "iisExpress": {
        "applicationUrl": "http://localhost:62789",
        "sslPort": 44326
      }
    },
    "profiles": {
      "http": {
        "commandName": "Project",
        "dotnetRunMessages": true,
        "launchBrowser": true,
        "inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}",
        "applicationUrl": "http://localhost:5154",
        "environmentVariables": {
          "ASPNETCORE_ENVIRONMENT": "Development"
        }
      },
      "https": {
        "commandName": "Project",
        "dotnetRunMessages": true,
        "launchBrowser": true,
        "inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}",
        "applicationUrl": "https://localhost:5000",
        "environmentVariables": {
          "ASPNETCORE_ENVIRONMENT": "Development"
        }
      },
      "IIS Express": {
        "commandName": "IISExpress",
        "launchBrowser": true,
        "inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}",
        "environmentVariables": {
          "ASPNETCORE_ENVIRONMENT": "Development"
        }
      }
    }
  }

The project is set to serve pages or views from the server app and the server app launches correctly. How do I find what is causing the unhandled exception?

Windows development | Internet Information Services
Developer technologies | ASP.NET | ASP.NET Core
{count} votes

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.