Deploy Blazor app to a IIS sub application

Viewed 2615

I am trying to deploy my Blazor Server application under /app which is a sub application in IIS. I read on several places online that I need to set the following:

Startup.cs | app.UsePathBase("/app")

and

_Host.cshtml | <base href="~/app/" />

But when I run this I get the following error:

blazor.server.js:15 [2020-05-26T16:16:11.796Z] Error: The circuit failed to initialize.
e.log @ blazor.server.js:15
blazor.server.js:1 [2020-05-26T16:16:11.798Z] Information: Connection disconnected.
blazor.server.js:1 Uncaught (in promise) Error: Invocation canceled due to the underlying connection being closed.
    at e.connectionClosed (blazor.server.js:1)
    at e.connection.onclose (blazor.server.js:1)
    at e.stopConnection (blazor.server.js:1)
    at e.transport.onclose (blazor.server.js:1)
    at e.close (blazor.server.js:1)
    at e.stop (blazor.server.js:1)
    at e.<anonymous> (blazor.server.js:1)
    at blazor.server.js:1
    at Object.next (blazor.server.js:1)
    at a (blazor.server.js:1)

If I change to (without trailing slash)

_Host.cshtml | <base href="~/app" />

The application runs but without any /app before the URL (no change as far as I can see).

On some example I saw that they changed the BlazorHub path but I just cannot get it to work. The reason behind this is to be able to host application and landing page as separate IIS sites.

2 Answers

In _host.chtml set base to

<base href="~/" />

Don't add app.UsePathBase("/app") in your pipeline

In IIS site menu Add Application. Bind it to directory where your app exists.

Set up new application pool for that SubApp ( No managed code )

To share Identity Cookie add

services.AddDataProtection()
.PersistKeysToFileSystem(new DirectoryInfo(@"D:\tmp\Keys"))
.ProtectKeysWithDpapi(protectToLocalMachine:true)
.SetApplicationName("SharedCookieApp");

services.ConfigureApplicationCookie(options =>
          {
              options.Cookie.Path = "/";

          });
Related