I am new to Razor Pages. I have just one page, I have Index.cshtml and Index.cshtml.cs with
public class IndexModel : PageModel
There is also _Layout.cshtml
.
I need to get username from HttpContext Header.
and show username in _Layout.cshtml
(there is navigation bar).
But I do not get where is the best place to put this code in Razor Pages?
I cannot put it into Index.cshtml.cs OnGet
, because I need in Layout, at least I think so.
This is outside this question, but FYI I use Azure Easy Auth and from documentation (and also other non Razor Pages projects) I see that HttpContext.Request.Headers["X-MS-CLIENT-PRINCIPAL-ID"]
would contain Name I need.
You can access the current HttpContext
inside of a Razor Page using the Context
property:
<p>@Context.Request.Headers["X-MS-CLIENT-PRINCIPAL-ID"]</p>
This approach is documented in Use HttpContext from a Razor view.