| name | frontend-razor-accessors |
| description | Type-safe static accessor pattern - 'Get' class for refactoring-safe routing (Get.Page, Get.Endpoint), permission checks (Get.Claim), and ViewData keys. Eliminates magic strings in Razor views and provides centralized navigation. Keywords: razor-pages, type-safety, navigation, routing, accessors, get-pattern, page-routing, endpoint-routing, refactoring-safety |
| last-updated | "2026-02-15T00:00:00.000Z" |
| difficulty | intermediate |
| tokens | ~1K |
Static Accessor Pattern (The 'Get' Class)
A pattern for providing type-safe, static access to routing, constants, and logic in Razor Views, avoiding "magic strings".
Replace Example* names with the concrete page, endpoint, controller, and program types discovered in the hosted project or repository profile.
When to Apply
- Creating a central entry point for application constants.
- Finding paths to pages or endpoints (
Get.Page...).
- Checking permissions or feature flags in Views (
Get.Claim...).
- Defining strongly-typed keys for extensive dictionaries like
ViewData.
The Get Class
Host a central static class (typically named Get) in the Hosted project. This class acts as a directory for various helper collections.
public static class Get
{
public static ExamplePageFor Page { get; } = PageCollectionBase<ExamplePageFor>.PageCollection;
public static ExampleEndpointFor Endpoint { get; } = (ExampleEndpointFor)EndpointCollectionBase<ExampleProgram>.EndpointCollection!;
public static RoleFor Role { get; } = new();
public static ClaimFor Claim { get; } = new();
public static ViewDataKeys ViewDataKeys { get; } = new();
}
Helper Collections (Dependency Construction)
Collections are used to group accessors logically (e.g., by feature or area).
Page Collection Pattern:
public class ExamplePageFor : PageCollectionBase<ExamplePageFor>
{
public RootPageFor Root { get; } = new();
public UserPageFor User { get; } = new();
}
Endpoint Collection Pattern:
public class ExampleEndpointFor : EndpointCollectionBase<ExampleProgram>
{
public ExampleUserApiFor User { get; } = new();
public ExampleQaApiFor QA { get; } = new();
}
Helper Categories
Type-Safe Routing (PageFor / EndpointFor)
Avoid hardcoding URL strings. Use helper classes that generate routes.
Pattern:
public class ExampleUserApiFor
{
public const string RouteTemplate = "Api/User/[controller]";
public ExampleUserIdentityLoginFor LoginController { get; } = new();
}
public class ExampleUserIdentityLoginFor : ControllerForBase<ExampleIdentityLoginController>
{
public ExampleUserIdentityLoginFor() : base(ExampleUserApiFor.RouteTemplate) { }
public ApiEndpoint Login { get; private set; } = null!;
}
Usage in Razor:
<!-- Refactoring-safe link generation -->
<a href="@Get.Page.User.Login">Login</a>
<form hx-post="@Get.Endpoint.User.LoginController.Login.Path()">
Logical Accessors (ClaimFor / FeatureFor)
Wrap complex logic (like ScopeContext checks) into readable properties.
Pattern:
public class ClaimFor
{
public bool CanDeleteQuestions => ScopeContext.HasClaim(Claims.DeleteQuestion);
}
Usage:
@if (Get.Claim.CanDeleteQuestions)
{
<button>Delete</button>
}
Dictionary Keys (ViewDataKeys)
If you use ViewData or TempData, define keys here to ensure sender and receiver use the same string.
public class ViewDataKeys
{
public string Title => "Title";
}
ViewData[Get.ViewDataKeys.Title] = "My Page";
Related Skills