natemcmaster / dotnet-serve

Simple command-line HTTPS server for the .NET Core CLI
https://nuget.org/packages/dotnet-serve/
Apache License 2.0
781 stars 65 forks source link

fallback-file doesn't work with Blazor webassembly #149

Open pekspro opened 1 month ago

pekspro commented 1 month ago

Describe the bug I'm trying to use fallback-file with a Blazor webassembly application. I get 404 when I'm loading URL:s that should return the fallback page.

This is related to https://github.com/natemcmaster/dotnet-serve/issues/104.

To Reproduce Steps to reproduce the behavior:

Create, publish and host a Blazor Webassembly application like this:

dotnet new create blazorwasm
dotnet publish
dotnet serve --port 5200 --directory bin/Release/net8.0/publish/wwwroot --fallback-file "bin/Release/net8.0/publish/wwwroot/index.html"

Navigate to http://localhost:5200/ and click on counter page. This works fine.

Now, navigate to http://localhost:5200/counter. 404 is returned, but the content in index.html was expected.

Logs

Starting server, serving bin\Release\net8.0\publish\wwwroot
Listening on:
  http://localhost:5200

Press CTRL+C to exit
info: Microsoft.AspNetCore.Hosting.Diagnostics[1]
      Request starting HTTP/1.1 GET http://localhost:5200/counter - -
info: Microsoft.AspNetCore.Routing.EndpointMiddleware[0]
      Executing endpoint 'Fallback {*path:nonfile}'
info: Microsoft.AspNetCore.Routing.EndpointMiddleware[1]
      Executed endpoint 'Fallback {*path:nonfile}'
info: Microsoft.AspNetCore.Hosting.Diagnostics[2]
      Request finished HTTP/1.1 GET http://localhost:5200/counter - - - 404 - text/html 10.6292ms
pekspro commented 1 month ago

I'm playing with the code and I think the correct way to the application should be:

dotnet serve --port 5200 --directory bin/Release/net8.0/publish/wwwroot --fallback-file index.html

But this will give me an error saying that index.html doesn't exists. I guess it looks in the current directory, not in the directory provided by the directory argument. Everything works if I remove the [FileExists] attribute on the FallbackFile property.

natemcmaster commented 1 month ago

Should we make the path relative to the value of --directory here?

https://github.com/natemcmaster/dotnet-serve/blob/fa21e93f390390cee5b6e5d440e9439a8b1b81f1/src/dotnet-serve/Startup.cs#L224C45-L224C66

pekspro commented 1 month ago

@natemcmaster, no, that is not necessary. It already uses works as it is :-) MapFallbackToFile is relative to the directory that is hosted as I understand it.