Reputation: 1
I have a Blazor Server .NET 8 web application that uses/calls a DLL for various tasks including retrieving and reading the value of a cookie. Basically, the DLL used HttpContext, which is not always available in Blazor.
Thank you, Max
I therefore tried to use JSRuntime, but I have to retrieve the cookie in the page initialization, so this solution isn't possible.
I tried using HttpContext only when it's available (pre-rendered) and saving a static variable from a class in my DLL, hoping that it would persist too, but that didn't work.
I also tried using the "Scoped" feature, without success.
I can't see any other solution at the moment... help!
The crazy thing is that I'm able to access my cookie during pre-rendering, my DLL is doing its job well. Then on the second page execution, the HttpContext disappears and I don't know how to retrieve the cookie.
Upvotes: 0
Views: 228
Reputation: 8616
HttpContext is always available using IHttpContextAccessor in blazor.
builder.Services.AddHttpContextAccessor();
@inject IHttpContextAccessor accessor
@code{
protected override void OnInitialized(){
var mycookies = accessor.HttpContext.Request.Cookies;
}
}
Upvotes: 0