Is it possible to configure Blazor Web Assembly .Net6 app to run through IDE and IIS?

Viewed 34

I'm very keen to be able to debug a WASM app through Visual Studio AND to test it through IIS, on different devices. Ideally this solution won't involve me having to swap settings or files manually. I have managed to achieve exactly this with a Blazor Server app but am struggling to do the same in WASM.

I have made a brand new Blazor Web Assembly .Net6 app through Visual Studio. It gives me the standard program.cs of:

using BlazorHelp;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;

var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("#app");
builder.RootComponents.Add<HeadOutlet>("head::after");

builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new     Uri(builder.HostEnvironment.BaseAddress) });

await builder.Build().RunAsync();`

I mention this because many solutions I've seen involve using, presumbly, a WebApplication.CreateBuiulder "builder" object rather than the WebAssemblyHostBuilder object. Possibly confusion with Blazor Server fixes.

At this point I can press play in the IDE and see the app running.

In the IDE I publish the app to a folder and left the default location as is. I copy this location (in the project's \bin\Release\net6.0\browser-wasm\publish folder) to the clipboard.

In IIS I make a new application, added the alias BlazorHelp, chosen my app pool and pasted the path above into the physical path. Before I can test the app through IIS I have to change the app's wwwroot/index.html with:

<base href="/BlazorHelp/" />

Once that's done I can see the app running through IIS but I am now unable to use Visual Studio to debug. When I swap back to:

<base href="/" />

it starts working again in the IDE but not through IIS.

Any clues as to what I'm missing would be most welcome.

1 Answers

To solve my example I copied the wwwroot/index.html to wwwroot/published_index.html and changed the:

<base href="/">

to:

<base href="/BlazorWASMTest/"> 

so that it matched up with my IIS settings.

Then I added the following to the project's .proj file:

<Target Name="PostPublishTask" AfterTargets="AfterPublish">
  <Message Text="Copying published_index.html to index.html" Importance="high" />
  <Exec Command="@ECHO F|XCOPY wwwroot\published_index.html bin\Debug\net6.0\browser-wasm\publish\wwwroot\index.html /f /y" />
</Target>

That copies the published_index.html from the project directory straight into my published directory and renames it to index.html. For local use this is exactly what I wanted. For deployment I may have to work out something a bit more sensible.

Related