Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

Blazor Server- IHttpContextAccessor.HttpContext.Features cannot be obtainedbecause HttpContext is null

$
0
0

I need to display a cookie consent message on my Blazor server page and following the solution explained on How to set consent cookie in Blazor Server. The solution works on the development machine, but when it's deployed on the app service, it fails with the following error.

Error: System.NullReferenceException: Object reference not set to an instance of an object.at CookiePolicyApp.Components.Layout._CookieConsentPartial.OnInitialized() in C:\Github\tsutomua\blazor\CookiePolicyApp\CookiePolicyApp\Components\Layout_CookieConsentPartial.razor:line 58at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync()

A simplified version of the original project (also updated for .NET 8) is posted on https://github.com/tsutomua/blazor/tree/main/CookiePolicyApp.

It appears that the error occurs because the HttpContext is null.

// From: _CookieConsentPartial.razor

ITrackingConsentFeature consentFeature;bool showBanner;string cookieString;protected override void OnInitialized(){    // hca is IHttpContextAccessor injected on the page.    consentFeature = hca.HttpContext.Features.Get<ITrackingConsentFeature>();    showBanner = !consentFeature?.CanTrack ?? false;    cookieString = consentFeature?.CreateConsentCookie();}

What would be a good way to solve this issue?The broken published page is https://cookiepolicyapp-d8a6ghddhhaqh0hv.westus-01.azurewebsites.net/


Viewing all articles
Browse latest Browse all 4839

Trending Articles