Home > OS >  Blazor Server Path Issues
Blazor Server Path Issues

Time:12-18

I hope someone can help I'm still pretty new to Blazor and I've been struggling with this problem all day.

I had a Blazor Server app that ran fine on my local machine and also fine on my deployment server (Ubuntu/with Apache proxy). It ran fine for months until I needed to move it from the root of the server to a subfolder e.g. https://myserver/myapp. That's when my nightmare began. Even though all the content paths are relative for css, images etc they were all trying to load from the root / of the website after I moved it.

There's several ways I have found for setting the path of the app.

app.UsePathBase("/myapp")
app.UseStaticFiles("/myapp")

Setting these seem to be completely ignored when deployed on the server for some reason so I had to look for other options. I then found that you can simply set the path in the tag in _Layout.cshtml:

<base href="/myapp/" />

This worked great... all the content loaded as intended and I thought it was fixed until I realised none of the dynamic js elements was working.

I tracked that down to a incorrect path in the script tag "/_framework/blazor.server.js" so I corrected the script tag to be "_framework/blazor.server.js".

Now its trying to load the script from https://myserver/myapp/_framework/blazor.server.js - the problem is that path does not exist! Yet somehow it works fine on my local dev environment.

I've tried many things... even copying the _framework folder from my local environment to wwwroot manually - which resulted in some strange errors and did not seem like a good idea.

I've also tried using ~/ as the base href which works locally but breaks on the server where it's just trying to serve everything from the root folder again.

I don't understand why things are working very differently in the development environment and the published server build. I also can't seem to change where that _framework folder is located and so can't load the necessary script files.

Is it something you do with the app.MapBlazorHub() command?

CodePudding user response:

Adding the code below to your _layout file configures the signalR hub to use an absolute path instead of a relative path:

<script src="~/_framework/blazor.server.js" autostart="false"></script>
<script>
    Blazor.start({
        configureSignalR: function (builder) {
            builder.withUrl("/_blazor");
        }
    });
</script>

Now, you can use your absolute path for it.

CodePudding user response:

In the end I worked around this by placing my app on it's own subdomain myapp.myserver.com. It doesn't really solve the original problem but Blazor Server apps just seem to work with much less hassle when they are in the located in the root path.

  • Related