Bolero icon indicating copy to clipboard operation
Bolero copied to clipboard

Remoting calls fail if base href has been set

Open Bananas-Are-Yellow opened this issue 3 years ago • 2 comments

Take the standard HelloWorld app.

(1) Change the base href in _Host.cshtml

<base href="/myrepo/">

(2) Change HelloWorld.Client.fsproj so that static resources will be found under /myrepo

  <PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
    <StaticWebAssetBasePath>myrepo</StaticWebAssetBasePath>
  </PropertyGroup>

Run the app and try to get a list of books from the Download data page. In the browser DevTools window, you will see:

POST http://localhost:5000/myrepo/books/getUsername 400 (Bad Request)

The books service has BasePath set to /books but this is relative to the base href, so the request gets made to /myrepo/books. The server uses AddRemoting<BookService>() which looks at the BasePath of the service but it does not know that the client has a base href set. So the service is not found.

I think we need another overload of AddRemoting that allows the client base href to be supplied as the prefix to use. (In my case, I need dependency injection so I could not see a workaround.)

Bananas-Are-Yellow avatar Feb 01 '21 17:02 Bananas-Are-Yellow

More information. If prerendered = true in AddBoleroHost (which is the default) and the initial page uses remoting, the prerendered page will look for the service on /books, so you need two service handler implementations, one for /books and one for /myrepo/books.

Bananas-Are-Yellow avatar Feb 07 '21 10:02 Bananas-Are-Yellow

Here is my workaround, which is to strip off the base href so that remoting does not use it.

module Program =
    [<EntryPoint>]
    let Main args =
        let builder = WebAssemblyHostBuilder.CreateDefault args
        builder.RootComponents.Add<App> "#main"

        // Old code:
        // builder.Services.AddRemoting builder.HostEnvironment |> ignore

        // New code adjusts the HttpClient.BaseAddress so that
        //     baseAddress = http://localhost:5000/myrepo/
        // becomes
        //     baseAddress = http://localhost:5000/

        builder.Services.AddRemoting (fun httpClient ->
            let baseHref = "myrepo/"
            let addr =
                let addr = builder.HostEnvironment.BaseAddress
                if addr.EndsWith baseHref then addr.Substring (0, addr.Length - baseHref.Length)
                else addr
            httpClient.BaseAddress <- Uri addr) |> ignore

        builder.Build().RunAsync() |> ignore
        0

Bananas-Are-Yellow avatar Feb 09 '21 15:02 Bananas-Are-Yellow