Aspnetcore: [Blazor] HttpClient error when url starts with '/'

Created on 19 Apr 2019  路  9Comments  路  Source: dotnet/aspnetcore

Describe the bug

Attempting to make an http call in blazor client-side, using a url that starts with "/", (e.g. "/api/GetValues") results in:
System.ArgumentException: Only 'http' and 'https' schemes are allowed.

To Reproduce

Steps to reproduce the behavior:

  1. Using ASP.NET Core 3.0 preview 4
  2. Create a new blazorhosted project
  3. Go to FetchData.razor and change the url from api/SampleData/WeatherForecasts to /api/SampleData/WeatherForecasts
  4. Run the app and navigate to /fetchdata

Screenshots

image

External area-blazor bug

Most helpful comment

I've investigated, and it is a Mono issue. Filed https://github.com/mono/mono/issues/14630 and closing as external.

Thanks to everyone who posted workaround suggestions.

All 9 comments

@SteveSandersonMS Is potentially an issue with our underlying HttpClientHandler? We're still not using the one from the Mono folks yet, correct?

Yes. We probably should fix this independently of moving to the Mono handler.

@Eilon hit this as well when updating Hubbup to Preview4.

I was migrating from Blazor 0.7.0 to ASP.NET Core 3.0 Preview 4 and hit this. I had to hard-code my fully qualified URL to get it to work:

c# var result = await Http.GetJsonAsync<IssueMoveData>($"https://localhost:44347/api/getmovedata/{fromOwner}/{fromRepo}/{fromIssueNumber}");

Otherwise the BaseAddress was the "current URL" in the browser, which had some pre-existing path segment (e.g. https://localhost:44347/Mover/), which would cause URLs to be resolved incorrectly.

Is there a workaround for this (besides just hardcoding the URL)?

I set the URL explicitly on the call to HttpClient. Slight;y annoying but it was only in a few places, so not too bad.

I was actually just able to inject a URIHelper and use _uriHelper.GetBaseUri() to construct the API calls.

public async Task<Something> Get()
{
    var client = new HttpClient();
    var baseUri = _uriHelper.GetBaseUri();
    if (!baseUri.EndsWith("/")) baseUrl += '/';
    var result = await client.GetJsonAsync<Something>($"{baseUri}api/somethings");
    return result;
}

I'm seeing the same issue following along with the workshop. According to the docs in https://github.com/dotnet-presentations/blazor-workshop/blob/master/docs/03-show-order-status.md#polling-for-order-details I'm supposed to use:

orderWithStatus = await HttpClient.GetJsonAsync<OrderWithStatus>($"/orders/{OrderId}");

Turns out, removing the first '/' on the route (in front of 'orders') fixes the issue:

orderWithStatus = await HttpClient.GetJsonAsync<OrderWithStatus>($"orders/{OrderId}");

I've investigated, and it is a Mono issue. Filed https://github.com/mono/mono/issues/14630 and closing as external.

Thanks to everyone who posted workaround suggestions.

Was this page helpful?
0 / 5 - 0 ratings