Add proper JSON-fetching example to StandaloneApp
This commit is contained in:
parent
314b5c339c
commit
1b0b5c61fe
|
|
@ -1,15 +1,53 @@
|
|||
@using Microsoft.AspNetCore.Blazor.Browser.Services.Temporary
|
||||
@inject HttpClient Http
|
||||
|
||||
<h1>Fetch data</h1>
|
||||
<h1>Weather forecast</h1>
|
||||
|
||||
<strong>Response: </strong> @responseText
|
||||
<p>This component demonstrates fetching data from the server.</p>
|
||||
|
||||
@if (forecasts == null)
|
||||
{
|
||||
<p><em>Loading...</em></p>
|
||||
}
|
||||
else
|
||||
{
|
||||
<table class='table'>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Date</th>
|
||||
<th>Temp. (C)</th>
|
||||
<th>Temp. (F)</th>
|
||||
<th>Summary</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@foreach (var forecast in forecasts)
|
||||
{
|
||||
<tr>
|
||||
<td>@forecast.DateFormatted</td>
|
||||
<td>@forecast.TemperatureC</td>
|
||||
<td>@forecast.TemperatureF</td>
|
||||
<td>@forecast.Summary</td>
|
||||
</tr>
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
}
|
||||
|
||||
@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<WeatherForecast[]>(json);
|
||||
}
|
||||
|
||||
class WeatherForecast
|
||||
{
|
||||
public string DateFormatted { get; set; }
|
||||
public int TemperatureC { get; set; }
|
||||
public int TemperatureF { get; set; }
|
||||
public string Summary { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
]
|
||||
|
|
@ -41,8 +41,9 @@ namespace Microsoft.AspNetCore.Blazor.Browser.Services
|
|||
|
||||
private void AddDefaultServices(ServiceCollection serviceCollection)
|
||||
{
|
||||
serviceCollection.AddSingleton<IUriHelper>(new BrowserUriHelper());
|
||||
serviceCollection.AddSingleton(new HttpClient());
|
||||
var uriHelper = new BrowserUriHelper();
|
||||
serviceCollection.AddSingleton<IUriHelper>(uriHelper);
|
||||
serviceCollection.AddSingleton(new HttpClient(uriHelper));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<int, TaskCompletionSource<HttpResponseMessage>> _pendingRequests
|
||||
= new Dictionary<int, TaskCompletionSource<HttpResponseMessage>>();
|
||||
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));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -61,7 +63,7 @@ namespace Microsoft.AspNetCore.Blazor.Browser.Services.Temporary
|
|||
/// <param name="requestUri">The URI the request is sent to.</param>
|
||||
/// <returns>A task representing the asynchronous operation.</returns>
|
||||
public Task<HttpResponseMessage> GetAsync(string requestUri)
|
||||
=> SendAsync(new HttpRequestMessage(HttpMethod.Get, requestUri));
|
||||
=> SendAsync(new HttpRequestMessage(HttpMethod.Get, CreateUri(requestUri)));
|
||||
|
||||
/// <summary>
|
||||
/// Sends a POST request to the specified URI and returns the response as
|
||||
|
|
@ -72,7 +74,7 @@ namespace Microsoft.AspNetCore.Blazor.Browser.Services.Temporary
|
|||
/// <param name="content">The content for the request.</param>
|
||||
/// <returns>A task representing the asynchronous operation.</returns>
|
||||
public Task<HttpResponseMessage> 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
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in New Issue