Hi @Hamed Vaziri
i've created class to get current context of the page (via injecting ihttpContextAccessor) & extract some information such as url, querystring, etc, Then save these data to database. The above class works correctly but i don't know where i should call it?
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, and HttpContext isn't guaranteed to hold the context that started the Blazor app.
So, to get the request url in Blazor application, you can try to use NavigationManager. Code like this:
@page "/counter"
@inject NavigationManager MyNavigationManager
<PageTitle>Counter</PageTitle>
<h1>Counter</h1>
<p role="status">Current count: @currentCount</p>
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
<button class="btn btn-primary" @onclick="RemoveItem">Remove Item</button>
@code {
private int currentCount = 0;
private void IncrementCount()
{
currentCount++;
}
protected override async Task OnInitializedAsync()
{
var result = MyNavigationManager.Uri;
// #> https://localhost:7031/counter/3?q=hi
var result2 = MyNavigationManager.BaseUri;
// #> https://localhost:7031/
}
}
The result as below:
Besides, if you want to get current user information in Blazor, you can use the AuthenticationStateProvider service.
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best regards,
Dillion