【问题标题】:Get user name in Razor Pages from HttpContext: where to put the code从 HttpContext 获取 Razor Pages 中的用户名:放置代码的位置
【发布时间】:2021-01-18 19:19:54
【问题描述】:

我是 Razor Pages 的新手。我只有一页,我有 Index.cshtml 和 Index.cshtml.cs 与

public class IndexModel : PageModel

还有_Layout.cshtml

我需要从HttpContext Header. 获取用户名并在_Layout.cshtml 中显示用户名(有导航栏)。

但我不知道将此代码放在 Razor Pages 中的最佳位置在哪里? 我不能把它放到Index.cshtml.cs OnGet,因为我需要在Layout中,至少我是这么认为的。


这不在这个问题范围内,但仅供参考,我使用 Azure Easy Auth 并从文档(以及其他非 Razor Pages 项目)中看到 HttpContext.Request.Headers["X-MS-CLIENT-PRINCIPAL-ID"] 将包含我需要的名称。

【问题讨论】:

    标签: c# asp.net-core razor-pages


    【解决方案1】:

    您可以使用 Context 属性访问 Razor 页面内的当前 HttpContext

    <p>@Context.Request.Headers["X-MS-CLIENT-PRINCIPAL-ID"]</p>
    

    这种方法记录在Use HttpContext from a Razor view

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-17
      • 2021-07-25
      • 2021-11-26
      • 2014-06-20
      • 2021-08-25
      • 2021-09-13
      • 2020-09-18
      相关资源
      最近更新 更多