'Blazor WASM IHttpContextAccessor
I have a vanilla Blazor Web Assembly project which I am trying to get access to the IHttpContextAccessor to get info like IP Address, Port number, URL scheme, subdomain, querystring etc... I have this working in Blazor Server but in Blazor WASM the HttpContext is always null
I added the Microsoft.AspNetCore.Http nuget package, and added builder.Services.AddHttpContextAccessor(); to the program.cs
I have looked at many examples online and all say to do it this way but for some reason cant get it to work. Is using the IHttpContextAccessor the way to go for web assembly or should I be using some other technique to get this kind of info?
Solution 1:[1]
MrC aka Shaun Curtis is right, this info is unavailable in the client. I believe the best way to do this would be to make an API on your server code (assuming you are hosting the WASM project with a .net core backend) and in that context you will have access to the HttpContext. The client will have no idea what your ip address is, but when you call an API that information is added to your request by (I believe) your ISP when it receives your request and forwards it to the service you are calling. So your server will know your ip address, then you can extract that info in the controller and then return it in the response. Or if you are deploying a standalone WASM app and have no backend, then you can always make an API call similar to this:
using (HttpClient client = new HttpClient())
{
var uri = new Uri("https://ipinfo.io/ip");
HttpResponseMessage response = await client.GetAsync(uri);
if (response.IsSuccessStatusCode)
{
var data = await response.Content.ReadAsStringAsync();
return data.Trim();
}
else
{
return "";
}
}
This is a copy and paste of something I did in a previous project and it worked quite well. It is very fast and simply returns the IP Address in the response body. Just make sure that you run this on the WASM app and not the server app otherwise you will end up with the server's IP and not the Client's.
Solution 2:[2]
To quote from an issue raised (and closed) on the ASPNetCore repository on Github:
This issue should be renamed, accessing the HttpContext in Blazor WASM doesn't make any sense, it's like trying to access the HttpContext in the react or angular. It runs on the client without access to this information.
The full issue is here - https://github.com/dotnet/aspnetcore/issues/22820
Solution 3:[3]
Better avoid those examples if that's what they are telling you to do: regardless of Blazor flavor (Server/WASM), you should never be accessing HttpContext outside the ASP.NET Core pipeline:
Additionally, again for security reasons, you must not use IHttpContextAccessor within Blazor apps. Blazor apps run outside of the context of the ASP.NET Core pipeline. The HttpContext isn't guaranteed to be available within the IHttpContextAccessor, nor is it guaranteed to be holding the context that started the Blazor app.
Furthermore, unlike Blazor Server, Blazor WASM apps run solely in the browser, which means your components aren't connecting to any server instance in the code behind; querying an HTTP Context doesn't make any sense because it doesn't exist.
Instead, you need to create a separate server-side app to communicate with your client-side application. You probably want to create a .NET Core Web API and migrate your HttpContext code there.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | JG222 |
| Solution 2 | MrC aka Shaun Curtis |
| Solution 3 | Connor Low |



