I have an ASP.Net Razor website, hosted on Azure. Setting up Cookie Consent Policy with this link: https://learn.microsoft.com/en-us/aspnet/core/security/gdpr?view=aspnetcore-7.0.
However, I am getting an error here:
var consentFeature = Context.Features.Get<ITrackingConsentFeature>();
The name 'Context' does not exist in current context. Everywhere I read implies it should just be available in my project, My page is in the shared folder, I am running ASP.Net Core 7.0. My full code is below:
@page
@model MYCompany.Pages.IndexPortalModel
@{
}
@using Microsoft.AspNetCore.Http.Features
@{
var consentFeature = Context.Features.Get<ITrackingConsentFeature>();
var showBanner = !consentFeature?.CanTrack ?? false;
var cookieString = consentFeature?.CreateConsentCookie();
}
@if (showBanner)
{
<div id="cookieConsent" class="alert alert-info alert-dismissible fade show" role="alert">
Use this space to summarize your privacy and cookie use policy. <a asp-page="/Privacy">Learn More</a>.
<button type="button" class="accept-policy close" data-bs-dismiss="alert" aria-label="Close" data-cookie-string="@cookieString">
<span aria-hidden="true">Accept</span>
</button>
</div>
<script>
(function () {
var button = document.querySelector("#cookieConsent button[data-cookie-string]");
button.addEventListener("click", function (event) {
document.cookie = button.dataset.cookieString;
}, false);
})();
</script>
}
I tried just replacing it with HttpContext which got rid of the error but gets null on page load. I checked all articles and ChatGPT, everything indicates Context should just be available. I followed article exactly, adding to Program.CS, layout, etc, please help and thank you.
this worked in my side.
The document asks you to create a partial view and using
Context
, we could see that it'sHttpContext
essentially.I test in my side after replacing 'Context' with
HttpContext
.