diff --git a/samples/StandaloneApp/Pages/FetchData.cshtml b/samples/StandaloneApp/Pages/FetchData.cshtml
index d41bd5607b..a14615eb57 100644
--- a/samples/StandaloneApp/Pages/FetchData.cshtml
+++ b/samples/StandaloneApp/Pages/FetchData.cshtml
@@ -1,15 +1,53 @@
@using Microsoft.AspNetCore.Blazor.Browser.Services.Temporary
@inject HttpClient Http
-
Fetch data
+Weather forecast
-Response: @responseText
+This component demonstrates fetching data from the server.
+
+@if (forecasts == null)
+{
+ Loading...
+}
+else
+{
+
+
+
+ | Date |
+ Temp. (C) |
+ Temp. (F) |
+ Summary |
+
+
+
+ @foreach (var forecast in forecasts)
+ {
+
+ | @forecast.DateFormatted |
+ @forecast.TemperatureC |
+ @forecast.TemperatureF |
+ @forecast.Summary |
+
+ }
+
+
+}
@functions {
- private string responseText;
+ WeatherForecast[] forecasts;
protected override async Task OnInitAsync()
{
- responseText = await Http.GetStringAsync("/");
+ var json = await Http.GetStringAsync("/sample-data/weather.json");
+ forecasts = Microsoft.AspNetCore.Blazor.Json.Deserialize(json);
+ }
+
+ class WeatherForecast
+ {
+ public string DateFormatted { get; set; }
+ public int TemperatureC { get; set; }
+ public int TemperatureF { get; set; }
+ public string Summary { get; set; }
}
}
diff --git a/samples/StandaloneApp/wwwroot/sample-data/weather.json b/samples/StandaloneApp/wwwroot/sample-data/weather.json
new file mode 100644
index 0000000000..2b8e8e3dd7
--- /dev/null
+++ b/samples/StandaloneApp/wwwroot/sample-data/weather.json
@@ -0,0 +1,32 @@
+[
+ {
+ "DateFormatted": "06/05/2018",
+ "TemperatureC": 1,
+ "Summary": "Freezing",
+ "TemperatureF": 33
+ },
+ {
+ "DateFormatted": "07/05/2018",
+ "TemperatureC": 14,
+ "Summary": "Bracing",
+ "TemperatureF": 57
+ },
+ {
+ "DateFormatted": "08/05/2018",
+ "TemperatureC": -13,
+ "Summary": "Freezing",
+ "TemperatureF": 9
+ },
+ {
+ "DateFormatted": "09/05/2018",
+ "TemperatureC": -16,
+ "Summary": "Balmy",
+ "TemperatureF": 4
+ },
+ {
+ "DateFormatted": "10/05/2018",
+ "TemperatureC": -2,
+ "Summary": "Chilly",
+ "TemperatureF": 29
+ }
+]
diff --git a/src/Microsoft.AspNetCore.Blazor.Browser/Services/BrowserServiceProvider.cs b/src/Microsoft.AspNetCore.Blazor.Browser/Services/BrowserServiceProvider.cs
index 11302a913d..52f7c1b5e9 100644
--- a/src/Microsoft.AspNetCore.Blazor.Browser/Services/BrowserServiceProvider.cs
+++ b/src/Microsoft.AspNetCore.Blazor.Browser/Services/BrowserServiceProvider.cs
@@ -41,8 +41,9 @@ namespace Microsoft.AspNetCore.Blazor.Browser.Services
private void AddDefaultServices(ServiceCollection serviceCollection)
{
- serviceCollection.AddSingleton(new BrowserUriHelper());
- serviceCollection.AddSingleton(new HttpClient());
+ var uriHelper = new BrowserUriHelper();
+ serviceCollection.AddSingleton(uriHelper);
+ serviceCollection.AddSingleton(new HttpClient(uriHelper));
}
}
}
diff --git a/src/Microsoft.AspNetCore.Blazor.Browser/Services/Temporary/HttpClient.cs b/src/Microsoft.AspNetCore.Blazor.Browser/Services/Temporary/HttpClient.cs
index c966e08ddf..2d964d6741 100644
--- a/src/Microsoft.AspNetCore.Blazor.Browser/Services/Temporary/HttpClient.cs
+++ b/src/Microsoft.AspNetCore.Blazor.Browser/Services/Temporary/HttpClient.cs
@@ -2,12 +2,12 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.AspNetCore.Blazor.Browser.Interop;
+using Microsoft.AspNetCore.Blazor.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
-using System.Net.Http.Headers;
using System.Threading.Tasks;
namespace Microsoft.AspNetCore.Blazor.Browser.Services.Temporary
@@ -25,6 +25,7 @@ namespace Microsoft.AspNetCore.Blazor.Browser.Services.Temporary
static int _nextRequestId = 0;
static IDictionary> _pendingRequests
= new Dictionary>();
+ IUriHelper _uriHelper;
// Making the constructor internal to be sure people only get instances from
// the service provider. It doesn't make any difference right now, but when
@@ -33,8 +34,9 @@ namespace Microsoft.AspNetCore.Blazor.Browser.Services.Temporary
// has to be configured with a browser-specific HTTP handler. In the long
// term, it should be possible to use System.Net.Http.HttpClient directly
// without any browser-specific constructor args.
- internal HttpClient()
+ internal HttpClient(IUriHelper uriHelper)
{
+ _uriHelper = uriHelper ?? throw new ArgumentNullException(nameof(uriHelper));
}
///
@@ -61,7 +63,7 @@ namespace Microsoft.AspNetCore.Blazor.Browser.Services.Temporary
/// The URI the request is sent to.
/// A task representing the asynchronous operation.
public Task GetAsync(string requestUri)
- => SendAsync(new HttpRequestMessage(HttpMethod.Get, requestUri));
+ => SendAsync(new HttpRequestMessage(HttpMethod.Get, CreateUri(requestUri)));
///
/// Sends a POST request to the specified URI and returns the response as
@@ -72,7 +74,7 @@ namespace Microsoft.AspNetCore.Blazor.Browser.Services.Temporary
/// The content for the request.
/// A task representing the asynchronous operation.
public Task PostAsync(string requestUri, HttpContent content)
- => SendAsync(new HttpRequestMessage(HttpMethod.Post, requestUri)
+ => SendAsync(new HttpRequestMessage(HttpMethod.Post, CreateUri(requestUri))
{
Content = content
});
@@ -98,7 +100,7 @@ namespace Microsoft.AspNetCore.Blazor.Browser.Services.Temporary
$"{typeof(HttpClient).FullName}.Send",
id,
request.Method.Method,
- request.RequestUri.ToString(),
+ ResolveRequestUri(request.RequestUri),
request.Content == null ? null : await GetContentAsString(request.Content),
SerializeHeadersAsJson(request));
@@ -119,6 +121,9 @@ namespace Microsoft.AspNetCore.Blazor.Browser.Services.Temporary
$"only supports contents of type {nameof(StringContent)}, but you supplied " +
$"{content.GetType().FullName}.");
+ private Uri CreateUri(String uri)
+ => new Uri(uri, UriKind.RelativeOrAbsolute);
+
private static void ReceiveResponse(
string id,
string responseDescriptorJson,
@@ -146,6 +151,9 @@ namespace Microsoft.AspNetCore.Blazor.Browser.Services.Temporary
}
}
+ private string ResolveRequestUri(Uri requestUri)
+ => _uriHelper.ToAbsoluteUri(requestUri.OriginalString).AbsoluteUri;
+
// Keep in sync with TypeScript class in Http.ts
private class ResponseDescriptor
{