You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add [RouteCache] attribute for per-route cache control
Introduces the [RouteCache] attribute to allow declarative, per-route caching configuration in Blazouter. Updates documentation (ATTRIBUTE_ROUTING.md, CACHING.md) with usage details and examples, adds a sample page demonstrating the attribute, and extends RouteAttributeDiscoveryService to support EnableCache from attributes.
Copy file name to clipboardExpand all lines: ATTRIBUTE_ROUTING.md
+96-56Lines changed: 96 additions & 56 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,10 +6,10 @@ Blazouter supports both programmatic route configuration (using `RouteConfig` ob
6
6
7
7
Attribute-based routing allows you to decorate your Blazor components with attributes that define their routing configuration. This approach:
8
8
9
-
-**✅ Declarative** - Clear, easy-to-read syntax
10
-
-**✅ Flexible** - Can be mixed with programmatic routes as needed
11
-
-**✅ More intuitive** - Route configuration is co-located with the component
12
-
-**✅ Backward compatible** - Works alongside existing programmatic routing
9
+
-**✅ Declarative** - Clear, easy-to-read syntax
10
+
-**✅ Flexible** - Can be mixed with programmatic routes as needed
11
+
-**✅ More intuitive** - Route configuration is co-located with the component
12
+
-**✅ Backward compatible** - Works alongside existing programmatic routing
13
13
14
14
## Available Attributes
15
15
@@ -28,7 +28,8 @@ public class AdminPage : ComponentBase
28
28
**Note:** Named `Route` instead of `Route` to avoid conflicts with Blazor's built-in `RouteAttribute`.
29
29
30
30
**Parameters:**
31
-
-`path` (string) - The route path pattern. Supports dynamic parameters with `:` prefix (e.g., `/users/:id`)
31
+
32
+
-`path` (string) - The route path pattern. Supports dynamic parameters with `:` prefix (e.g., `/users/:id`)
32
33
33
34
### `[RouteTransition]` - Set Animation
34
35
@@ -44,7 +45,8 @@ public class AboutPage : ComponentBase
44
45
```
45
46
46
47
**Parameters:**
47
-
-`transition` (RouteTransition enum) - The animation type (Fade, Slide, Scale, etc.)
48
+
49
+
-`transition` (RouteTransition enum) - The animation type (Fade, Slide, Scale, etc.)
48
50
49
51
### `[RouteMiddleware]` - Add Route Middleware
50
52
@@ -62,16 +64,18 @@ public class AdminPage : ComponentBase
62
64
```
63
65
64
66
**Parameters:**
65
-
-`middlewareType` (Type) - The type of the middleware class (must implement `IRouteMiddleware`)
67
+
68
+
-`middlewareType` (Type) - The type of the middleware class (must implement `IRouteMiddleware`)
66
69
67
70
**Note:** Middleware execute in the order they are declared, before guards. Middleware can execute code before and after navigation, share data with components, and abort or redirect navigation.
68
71
69
72
**Common Use Cases:**
70
-
- Logging and analytics tracking
71
-
- Performance monitoring
72
-
- Data preloading
73
-
- Feature flags
74
-
- Session management
73
+
74
+
- Logging and analytics tracking
75
+
- Performance monitoring
76
+
- Data preloading
77
+
- Feature flags
78
+
- Session management
75
79
76
80
### `[RouteGuard]` - Add Access Control
77
81
@@ -88,7 +92,8 @@ public class AdminPage : ComponentBase
88
92
```
89
93
90
94
**Parameters:**
91
-
-`guardType` (Type) - The type of the guard class (must implement `IRouteGuard`)
95
+
96
+
-`guardType` (Type) - The type of the guard class (must implement `IRouteGuard`)
92
97
93
98
**Note:** Guards execute in the order they are declared, after middleware.
94
99
@@ -106,7 +111,8 @@ public class AdminPage : ComponentBase
106
111
```
107
112
108
113
**Parameters:**
109
-
-`layoutType` (Type?) - The layout component type (must inherit from `LayoutComponentBase`), or `null` for no layout
114
+
115
+
-`layoutType` (Type?) - The layout component type (must inherit from `LayoutComponentBase`), or `null` for no layout
110
116
111
117
### `[RouteTitle]` - Set Page Title
112
118
@@ -122,7 +128,8 @@ public class AboutPage : ComponentBase
122
128
```
123
129
124
130
**Parameters:**
125
-
-`title` (string) - The route title
131
+
132
+
-`title` (string) - The route title
126
133
127
134
### `[RouteData]` - Add Custom Data
128
135
@@ -137,14 +144,15 @@ public class AdminPage : ComponentBase
137
144
// Only define parameters you need - others are automatically filtered
138
145
[Parameter]
139
146
publicstring? Section { get; set; }
140
-
147
+
141
148
// RequireAdmin is not defined, so it's filtered out (no error)
142
149
}
143
150
```
144
151
145
152
**Parameters:**
146
-
-`key` (string) - The data key
147
-
-`value` (object) - The data value
153
+
154
+
-`key` (string) - The data key
155
+
-`value` (object) - The data value
148
156
149
157
**Note:** Route data is automatically filtered based on component parameters. You can use any `[RouteData]` attributes without needing matching parameters in the component - only data with matching `[Parameter]` properties will be passed through.
150
158
@@ -162,7 +170,8 @@ public partial class OldPathRedirect : ComponentBase
162
170
```
163
171
164
172
**Parameters:**
165
-
-`redirectPath` (string) - The target redirect path
173
+
174
+
-`redirectPath` (string) - The target redirect path
166
175
167
176
**Note:** When using `RouteRedirect`, the component will not be rendered, and other attributes like `RouteTransition` or `RouteGuard` will be ignored.
168
177
@@ -180,10 +189,37 @@ public partial class ProductsPage : ComponentBase
180
189
```
181
190
182
191
**Parameters:**
183
-
-`exact` (bool) - Whether the route path must match the URL exactly (defaults to `true`)
192
+
193
+
-`exact` (bool) - Whether the route path must match the URL exactly (defaults to `true`)
184
194
185
195
**Note:** When `false` (default behavior without the attribute), routes with child routes can match partially. Use `[RouteExact(true)]` for routes that should only match the exact path.
186
196
197
+
### `[RouteCache]` - Control Route Caching
198
+
199
+
Specifies whether caching is enabled for this specific route.
200
+
201
+
```csharp
202
+
[Route("/admin/dashboard")]
203
+
[RouteCache(false)]
204
+
publicpartialclassAdminDashboard : ComponentBase
205
+
{
206
+
// This route will never be cached
207
+
}
208
+
209
+
[Route("/static-page")]
210
+
[RouteCache(true)]
211
+
publicpartialclassStaticPage : ComponentBase
212
+
{
213
+
// This route will always be cached, even if global caching is disabled
214
+
}
215
+
```
216
+
217
+
**Parameters:**
218
+
219
+
-`enableCache` (bool?) - Whether to cache this route. `true` = always cache, `false` = never cache, `null` = use global settings (default)
220
+
221
+
**Note:** This affects route match caching. For more information about caching, see [CACHING.md](CACHING.md).
|`ComponentLoader`| ❌ No | N/A | Requires async lambda - use programmatic config |
576
+
|`Children`| ❌ No | N/A | Complex hierarchies - use programmatic config |
577
+
578
+
**Coverage:** 11 out of 12 `RouteConfig` properties are supported via attributes (92% coverage).
540
579
541
580
The two unsupported properties (`ComponentLoader` and `Children`) require complex programmatic logic that cannot be expressed declaratively through attributes. For these scenarios, use traditional programmatic `RouteConfig` objects.
542
581
@@ -549,8 +588,9 @@ The two unsupported properties (`ComponentLoader` and `Children`) require comple
549
588
### Q: Can I use both `@page` and `[Route]` on the same component?
550
589
551
590
**A:** While technically possible, it's not recommended. Choose one routing approach:
552
-
- Use `@page` for Blazor's built-in routing
553
-
- Use `[Route]` for Blazouter's enhanced routing
591
+
592
+
- Use `@page` for Blazor's built-in routing
593
+
- Use `[Route]` for Blazouter's enhanced routing
554
594
555
595
### Q: Do attribute-based routes break existing code?
556
596
@@ -578,6 +618,6 @@ Check out the [AttributeRouting.razor](samples/Blazouter.WebAssembly.Sample/Page
0 commit comments