Hi,
In ASP.NET Core, HttpContext.Request.Url was replaced with properties like HttpContext.Request.Scheme, HttpContext.Request.Host, HttpContext.Request.Path, and HttpContext.Request.QueryString. You need to combine these to build the full URL.In Blazor WebAssembly, there is no HttpContext at all because the code executes in the browser. Instead, Blazor gives you NavigationManager to read and manipulate the current URI ASP.NET Core Blazor routing and navigation | Microsoft Learn
- In Blazor Server, you can access
HttpContext, but only when you flow it into your service properly. - In Blazor WebAssembly, there is no server
HttpContext, so you need to useNavigationManagerto obtain the current URL.
For Blazor Server
Inject IHttpContextAccessor into your service and construct the URL like this:
using Microsoft.AspNetCore.Http;
public class MyService
{
private readonly IHttpContextAccessor _httpContextAccessor;
public MyService(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
}
public string? GetAbsoluteUrl()
{
var request = _httpContextAccessor.HttpContext?.Request;
if (request == null) return null;
return $"{request.Scheme}://{request.Host}{request.Path}{request.QueryString}";
}
}
Don’t forget to register the accessor in Program.cs:
builder.Services.AddHttpContextAccessor();
If you’d like more details about working with HttpContext in ASP.NET Core, Microsoft explains it here: Access HttpContext in ASP.NET Core | Microsoft Learn.
For Blazor WebAssembly
Use NavigationManager (it’s built-in for exactly this purpose):
using Microsoft.AspNetCore.Components;
public class MyService
{
private readonly NavigationManager _navigationManager;
public MyService(NavigationManager navigationManager)
{
_navigationManager = navigationManager;
}
public string GetAbsoluteUrl()
{
return _navigationManager.Uri; // full URL of the current page
}
}
Hope this helps, feel free to reach out if there's any problem