How do I access HttpContext in Server-side Blazor?

Add the following to Blazor.Web.App.Startup.cs: services.AddHttpContextAccessor(); You also need this in <component-name>.cshtml @using Microsoft.AspNetCore.Http @inject IHttpContextAccessor httpContextAccessor Note: At the time when this answer was written, accessing the HttpContext was done as described above. Since then, Blazor has been under rapid development, and has fundamentally changed. It is definitely deprecated the usage described above, but … Read more

Best way to share data between two child components in Blazor

You may define a class service that implements the State pattern and the Notifier pattern to handle the state of your objects, pass state to objects, and notify subscriber objects of changes. Here’s a simplified example of such service, which enables a parent component to communicate with his children. NotifierService.cs public class NotifierService { private … Read more

Blazor Timer call async API task to update UI

You probably don’t want to Invoke() the GetValue(), that would be rather pointless. You can implement the timer like this: System.Threading.Timer timer; protected override void OnInitialized() { timer = new System.Threading.Timer(async _ => // async void { Time = await GetValue(); // we need StateHasChanged() because this is an async void handler // we need … Read more

Get Current User in a Blazor component

There are three possibilities for getting the user in a component (a page is a component): Inject IHttpContextAccessor and from it access HttpContext and then User; need to register IHttpContextAccessor in Startup.ConfigureServices, normally using AddHttpContextAccessor. Edit: according to the Microsoft docs you must not do this for security reasons. Inject an AuthenticationStateProvider property, call GetAuthenticationStateAsync … Read more

How do I get the access token from a blazor (server-side) web app?

The following code snippets provide a way to retrieve the access token issued when a user is authenticated with IdentityServer4 provider. In order to get the access token you can use the HttpContext object, but since Blazor is SignalR-based, you’ll have to do it the only time the HttpContext object is available, when the connection … Read more

How can one generate and save a file client side using Blazor?

Creator of Blazor Steve Sanderson used JavaScript interop for similar task during one of his last presentations. You can find example on BlazorExcelSpreadsheet Solution includes three parts: 1) JavaScript function saveAsFile(filename, bytesBase64) { var link = document.createElement(‘a’); link.download = filename; link.href = “https://stackoverflow.com/questions/52683706/data:application/octet-stream;base64,” + bytesBase64; document.body.appendChild(link); // Needed for Firefox link.click(); document.body.removeChild(link); } 2) C# … Read more

NavigationManager – Get current URL in a Blazor component

Use the Uri property from the NavigationManager class. How it works Get it from injection before using it on .razor pages: @inject NavigationManager MyNavigationManager Or like this in a .cs file if you prefer the “code-behind” experience: using Microsoft.AspNetCore.Components; // … [Inject] public NavigationManager MyNavigationManager {get; set;} = default!; Sample @page “/navigate” @inject NavigationManager MyNavigationManager … Read more

How to make two-way binding on Blazor component

Quick answer Quoting Blazor docs: Component parameters Binding recognizes component parameters, where @bind-{property} can bind a property value across components. For your page: <EditForm Model=”model” OnValidSubmit=”Submit”> <MyInputComponent @bind-BindingValue=”model.Name” /> </EditForm> The child component MyInputComponent: <div> <InputText type=”text” @bind-Value=”@BindingValue” /> </div> @code { private string _value; [Parameter] public string BindingValue { get => _value; set { … Read more

Get current URL in a Blazor component

Use the Uri property from the NavigationManager class. How it works Get it from injection before using it on .razor pages: @inject NavigationManager MyNavigationManager Or like this in a .cs file if you prefer the “code-behind” experience: using Microsoft.AspNetCore.Components; // … [Inject] public NavigationManager MyNavigationManager {get; set;} = default!; Sample @page “/navigate” @inject NavigationManager MyNavigationManager … Read more