ASP.NET Core/Razor Pages Cookie 同意,“上下文”在当前上下文中不存在

ASP.NET Core/Razor Pages Cookie Consent, 'Context' Does not exist in current context

提问人:CJ-44 提问时间:11/6/2023 最后编辑:egleaseCJ-44 更新时间:11/7/2023 访问量:54

问:

我有一个托管在 Azure 上的 ASP.Net Razor 网站。使用此链接设置 Cookie 同意策略:https://learn.microsoft.com/en-us/aspnet/core/security/gdpr?view=aspnetcore-7.0

但是,我在这里收到一个错误:

 var consentFeature = Context.Features.Get<ITrackingConsentFeature>();

名称“Context”在当前上下文中不存在。我读到的任何地方都暗示它应该在我的项目中可用,我的页面在共享文件夹中,我正在运行 ASP.Net Core 7.0。我的完整代码如下:

@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>
}

我尝试用 HttpContext 替换它,它摆脱了错误,但在页面加载时为 null。我检查了所有文章和 ChatGPT,一切都表明 Context 应该可用。我完全按照文章进行了操作,添加了程序.CS,布局等,请帮助并谢谢。

asp.net asp.net-core cookie razor cookieconsent

评论

0赞 pcalkins 11/7/2023
删除“@page @model MYCompany.Pages.IndexPortalModel @{ }”,我相信它应该可以工作。“@using Microsoft.AspNetCore.Http.Features”应为第一行。
1赞 CJ-44 11/7/2023
这奏效了!谢谢!

答:

0赞 Tiny Wang 11/7/2023 #1

将其替换为 HttpContext,它消除了错误但得到 null 页面加载时

这在我这边奏效了。

该文档要求您创建一个部分视图,并使用 ,我们可以看到它本质上是。ContextHttpContext

enter image description here

将“Context”替换为 .HttpContext

enter image description here

@page
@model WebAppRazor.Pages.Clients.IndexModel
@using Microsoft.AspNetCore.Http.Features

@{
    var consentFeature = HttpContext.Features.Get<ITrackingConsentFeature>();
    // var consentFeature = Context.Features.Get<ITrackingConsentFeature>();
    var showBanner = !consentFeature?.CanTrack ?? false;
    var cookieString = consentFeature?.CreateConsentCookie();
}

<div>
    @if (showBanner)
    {
        <div>@consentFeature.CanTrack</div>
        <div>@consentFeature.HasConsent</div>
        <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>
    }
</div>

评论

0赞 CJ-44 11/7/2023
切换到 HttpContext 时,我在将页面加载到 indexmodel 时得到:System.NullReferenceException:'对象引用未设置为对象的实例。Microsoft.AspNetCore.Mvc.RazorPages.PageBase.HttpContext.get 返回 null。
0赞 Tiny Wang 11/8/2023
嗨,@CJ-44,您能否分享一个最小的样本来重现问题并对其进行故障排除?由于文档后面的代码在我这边工作......