dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.55k stars 10.05k forks source link

Blazor different base path produces error #57843

Closed roblangston closed 2 months ago

roblangston commented 2 months ago

Is there an existing issue for this?

Describe the bug

When you create a standard Blazor server app via the template, you receive an error if you have changed the base path within the configuration when trying to increment the counter.

page

Its a similar problem to that mentioned here: https://github.com/dotnet/aspnetcore/issues/54469. This base path behaviour is different between frameworks .Net 8 and .Net 9. In .Net 8, the blazor.web.js attempts to be downloaded from http://localhost:5165/_framework/blazor.web.js even if a different base path "blazor" has been set. In .Net 9, the file is downloaded from http://localhost:5165/blazor/_framework/blazor.web.js correctly but a different error is received when trying to interact with page components (see error).

error

The following lines have been changed in the program.cs:-

app.UsePathBase("/blazor/"); app.MapBlazorHub("/blazor"); app.UseStaticFiles("/blazor");

In App.razor, the base has been changed as follows:-

<base href="/blazor/" />

The various page routes have been prefixed to match the base path as follows:-

@page "/blazor/counter"

The reason for wanting this to work correctly is to be able to front multiple sites behind a reverse proxy, either Yarp or NginX and that behavior doesnt seem to work without the sites being deployed with a different base path.

Expected Behavior

Page functions correctly when having base path changed.

Steps To Reproduce

Create a new Blazor web project from within VS2022 Preview, make changes to the code as detailed above in the description.

GitHub repo here:- https://github.com/roblangston/BlazorTest

Exceptions (if any)

blazor.web.js:1 [2024-09-12T18:09:34.506Z] Information: Normalizing '_blazor' to 'http://localhost:5165/blazor/_blazor'. _blazor/negotiate?negotiateVersion=1:1

   Failed to load resource: the server responded with a status of 405 (Method Not Allowed)

blazor.web.js:1 [2024-09-12T18:09:34.509Z] Error: Failed to complete negotiation with the server: Error: Method Not Allowed: Status code '405' log @ blazor.web.js:1 blazor.web.js:1 [2024-09-12T18:09:34.510Z] Error: Failed to start the connection: Error: Failed to complete negotiation with the server: Error: Method Not Allowed: Status code '405' log @ blazor.web.js:1 blazor.web.js:1 [2024-09-12T18:09:34.510Z] Error: Error: Failed to complete negotiation with the server: Error: Method Not Allowed: Status code '405' log @ blazor.web.js:1 blazor.web.js:1 Uncaught (in promise) zt: Failed to complete negotiation with the server: Error: Method Not Allowed: Status code '405' at Dn._getNegotiationResponse (http://localhost:5165/blazor/_framework/blazor.web.js:1:87758) at async Dn._startInternal (http://localhost:5165/blazor/_framework/blazor.web.js:1:85614) at async Dn.start (http://localhost:5165/blazor/_framework/blazor.web.js:1:83488) at async wn._startInternal (http://localhost:5165/blazor/_framework/blazor.web.js:1:58614) at async wn._startWithStateTransitions (http://localhost:5165/blazor/_framework/blazor.web.js:1:58028) at async Bo.startConnection (http://localhost:5165/blazor/_framework/blazor.web.js:1:136200) at async Bo.startCore (http://localhost:5165/blazor/_framework/blazor.web.js:1:134228) at async Zo (http://localhost:5165/blazor/_framework/blazor.web.js:1:153050)

.NET Version

9.0.100-rc.1.24452.12

Anything else?

1

javiercn commented 2 months ago

@roblangston thanks for contacting us.

I've tracked this down to some development specific functionality being wired-up when the container is running. This fix https://github.com/dotnet/aspnetcore/pull/57671 we did recently should take care of ensuring that functionality is not enabled in this scenario.

It was backported to RC2 a few days ago https://github.com/dotnet/aspnetcore/pull/57764

In the meantime, you can use "ReloadStaticAssetsAtRuntime": false to disable the problematic behavior in your appSettings.Development.json file.

javiercn commented 2 months ago

Here is also a dockerfile if you want to run against RC2

ARG IMAGE_VERSION=9.0-preview
FROM mcr.microsoft.com/dotnet/sdk:${IMAGE_VERSION} AS install-nightly
ARG DOTNET_CHANNEL=9.0.1.xx
ARG DOTNET_NIGHTLY_VERSION=9.0.100-rc.2.24463.6
ARG DOTNET_INSTALL_DIR=/.dotnet
RUN mkdir -p $DOTNET_INSTALL_DIR
RUN command curl -sSL https://dot.net/v1/dotnet-install.sh | bash /dev/stdin --channel $DOTNET_CHANNEL --install-dir $DOTNET_INSTALL_DIR ${DOTNET_NIGHTLY_VERSION:+--version $DOTNET_NIGHTLY_VERSION}
RUN mkdir -p /src
WORKDIR /src

FROM install-nightly AS workspace
ENV DOTNET_ROOT=$DOTNET_INSTALL_DIR
ENV PATH=${DOTNET_INSTALL_DIR}:$PATH

FROM workspace AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
RUN dotnet new nugetconfig
RUN dotnet nuget add source https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet9/nuget/v3/index.json -n dotnet9 --configfile /src/nuget.config
RUN cat /src/nuget.config
COPY ["BlazorTest/BlazorTest.csproj", "BlazorTest/"]
RUN dotnet restore "./BlazorTest/BlazorTest.csproj"
COPY . .
WORKDIR "/src/BlazorTest"
RUN dotnet build "./BlazorTest.csproj" -c $BUILD_CONFIGURATION -o /app/build

# This stage is used to publish the service project to be copied to the final stage
FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "./BlazorTest.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false

# This stage is used in production or when running from VS in regular mode (Default when not using the Debug configuration)
FROM workspace AS final
WORKDIR /src
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "BlazorTest.dll"]
javiercn commented 2 months ago

Just validated myself that this is fixed in RC2.

javiercn commented 2 months ago

Closing as dupe of https://github.com/dotnet/AspNetCore-ManualTests/issues/3007

roblangston commented 2 months ago

Closing as dupe of https://github.com/dotnet/AspNetCore-ManualTests/issues/3007

Is the issue referenced above internal, I get a 404 when trying to visit the link.