Warm tip: This article is reproduced from serverfault.com, please click

其他-代码:在我的html文件中键入c#时,“ Request.Cookies”无效

(其他 - Code: "Request.Cookies" is invalid when typing c# in my html file)

发布于 2020-11-29 19:56:13

我正在尝试获取Cookie的值,并根据该值在页面顶部的导航栏中显示不同的链接。这是一个ASP.NET Core MVC 3.1应用程序。我收到一个错误,该词Request在此上下文中无效。有没有办法使该代码正常工作,还是需要其他代码才能获得该价值?在我的启动文件中,我有以下代码:app.UseCookiePolicy();

<li class="nav-item">
    @{string signedUp = Request.Cookies["signedUp"]; } 
 
    @*if user's first time, show signup link, otherwise, then the if else below...*@

    @if (User.Identity.IsAuthenticated)
    {
        <a class="nav-link text-dark" asp-area="" asp-controller="Account" asp-action="LogOut">Log Out</a>
    }
    else
    {
        <a class="nav-link text-dark" asp-area="" asp-controller="Account" asp-action="LogIn">Log In</a>
    }
</li>
Questioner
mamaj
Viewed
11
Jackdaw 2020-11-30 05:28:43

剃须刀页面具有HttpContext和,它们Request是的一部分Context

@{ string signedUp = Context.Request.Cookies["signedUp"]; }

https://docs.microsoft.com/zh-cn/dotnet/api/microsoft.aspnetcore.http.httpcontext.request