How/Where can I declare an object like @User so that it can be referenced globally in any razor view using @?
Something similar to:
_LoginPartial.cshtml:
@if(Request.IsAuthenticated) {
<text>Signed In As <strong>@User.Identity.Name</strong> |
but instead reference my object:
@if(this && that) { <text>@MyObject.GetData</text> }
You can change the base type of a Razor page to a one of your own eg:
And then modify your config file like so:
Phil Haack has a very good blog post on this here.
Alternatively, you can add an extension method to
System.Web.Mvc.WebViewPage(the base type for razor pages) and use this.Which would be useable like so:
Personally, I prefer the first approach, but thought I’d provide the second for an alternative option.