#SharePointProblems | Koskila.net

Solutions are worthless unless shared! Antti K. Koskela's Personal Professional Blog
>

HttpClient in Blazor

koskila
Reading Time 3 min
Word Count 514 words
Comments 3 comments
Rating 5 (1 votes)
View

Blazor is a new (ish) framework for building web UIs with C#. It either uses SignalR to manage connections between your client-side and server-side code or even compiles directly to WebAssembly, in both cases cutting out the need to write any pesky JavaScript yourself - pretty neat, if you ask me!

However, developing your web apps with Blazor is a bit different from using ASP.NET MVC, for example. Other people have written great introductions to the tech, so I'll stick to the problem at hand: Using HttpClient with Blazor (Server).

Description

With Blazor, a lot of your code runs serverside, and you're using server-side dependencies. One example of this is HTTP queries, to which you need the class HttpClient. However, not all of the "classic" web server stack you might've grown to expect is available without configuration - and HttpClient is probably the most obvious dependency that'll require an additional step to get it up and running.

So - Blazor & HttpClient. Let's get to it!


Solution

Time needed: 5 minutes.

How to use HttpClient in your Blazor app?

  1. Configure your app

    Server Side Blazor doesn't register HttpClient by default, so you need to add it. A code snippet showing you how to do that is available here.

  2. Use it in your code (Blazor pages or such)

    You can now inject it and use it in your code. See below for a code sample on how to do that!

Sample 1: Configuring your app to support HttpClient

For .NET Core 3 (starting from preview 9), this is how you can configure the HttpClient for your Blazor app:

// Server Side Blazor doesn't register HttpClient by default
if (services.All(x => x.ServiceType != typeof(HttpClient)))
{
	services.AddScoped(
		s =>
		{
			var navigationManager = s.GetRequiredService<NavigationManager>();
			return new HttpClient
			{
				BaseAddress = new Uri(navigationManager.BaseUri)
			};
		});
}

And for .NET Core 3.0 versions before preview 9 UriHelper is used instead of NavigationManager. It's essentially the same thing in this case - so I just replaced the requested service to be that.

if (!services.Any(x => x.ServiceType == typeof(HttpClient)))
{
	// Setup HttpClient for server side in a client side compatible fashion
	services.AddScoped<HttpClient>(s =>
	{
		// Creating the URI helper needs to wait until the JS Runtime is initialized, so defer it.
		var uriHelper = s.GetRequiredService<IUriHelper>();
		return new HttpClient
		{
			BaseAddress = new Uri(uriHelper.GetBaseUri())
		};
	});
}

Sample 2: Using a HttpClient on your Blazor pages and components

The way this works in your code, no matter which version of ASP.NET Core you're on, is somewhat like this:

@inject HttpClient Http

...

@code {

 string yourAPIUrl = "https://yourapisite.azurewebsites.net/api/getdata";

 protected override async Task OnInitializedAsync()
 {
  // do something with your HttpClient - in this case, GET data from your custom API...
  var t = Http.GetAsync(yourAPIUrl);
  
  ...

 }
}

Pretty easy and straightforward!


A quick detour here - UriHelper injection and name have changed back and forth a few times. This post below should explain that a bit more:

https://www.koskila.net/how-to-use-urihelper-or-navigationmanager-in-net-core-3-0-blazor/

Just another bit of documentation that's tough to maintain :)

References

Comments

Interactive comments not implemented yet. Showing legacy comments migrated from WordPress.
Gerard
2019-10-22 12:30:33)
Antti, I have been struggling and searching since the release of 3.0 with trying to get "HttpClient.GetJsonAsync" to compile. All I had to do was use "HttpClient.GetAsync". It is my opinion that the changes in this release were not properly explained. THANK YOU!
Gerard
2019-10-22 13:43:45)
One comment Antti, In your example you may want to show the following in order to consume the result: var t = Http.GetAsync(yourAPIUrl); t.EnsureSuccessStatusCode(); string responseBody = await t.Content.ReadAsStringAsync();
2019-10-25 06:30:10
Thanks for the feedback, Gerard! You bring up a valid point. The code could definitely be expanded.