c# - 代码:在我的 html 文件中键入 c# 时,“Request.Cookies”无效
问题描述
我正在尝试获取 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>
解决方案
剃须刀页面有HttpContext
并且Request
是 的一部分Context
:
@{ string signedUp = Context.Request.Cookies["signedUp"]; }
https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.http.httpcontext.request
推荐阅读
- python - 如何在pandas df中选择值也出现在过滤列中的行?
- javascript - 有没有办法在不重叠的情况下在绘图工具提示中显示文本?
- objective-c - 如何在 Objective-C 中缓存图像
- amazon-web-services - Kafka: Understanding Broker failure
- javascript - Clicking on next button it saves form.But once I come back to previous page and again click on same next button it shows validation error message
- javascript - 未找到 React 模块 - 如何在反应中导入模块
- html - 在 Django 视图和模板中执行一些算术后,我得到了错误的值
- python - 为什么在 __exit__ 函数中更改引用不起作用?
- laravel - Laravel 返回注册 ID
- ios - 不要在选项卡开关的导航中重新加载顶部视图中的内容