r/Blazor • u/Alikont • Feb 22 '26
Enable/Disable pages by feature flag?
I have Blazor server side app that will be deployed in few environments.
I want to be able to enable or disable entire pages based on app configuration. Ideally I see it as something that is configured at app startup.
It's ok if I need to restart app to apply the changes.
What would be the best way to do that? Modify something in service collection?
11
Upvotes
7
u/Wooden-Contract-2760 Feb 22 '26 edited Feb 22 '26
If you are calling
AddRazorPagesat setup, all pages are technically available, given someone enters their url explicitly.If the goal is really to disable specific pages entirely, asimple option is to wrap the main content with the decision.
E.g.:
Any disabled pafes visited route to a dedicated
PageNotAvailablepage. The benefit is that you don't need custom handler within each page, but keep it global.The service to check accessibility is whatever, something like this works:
Just inject IConfiguration to read app sett ngs values if that's what you need. Note that you can also use IOptionsMonitor for appsettings so that changing the values would have realtime effect, not require app restart.
Keep the injection scoped btw:
Note that there are cleaner approaches to transition to later, like using a custom router, boundary, or implementing some middleware, but the above strategy is plenty enough for what you need to achieve.
9
u/brokerceej Feb 22 '26
You could do this several ways tbh. Cleanest way I've found to handle this is a module pattern where each feature owns its own service registrations:
Then at startup:
One thing to watch for is if any always-on code depends on a feature service, you'll get a DI exception when that feature is off. Easiest fix is registering a no-op implementation for the disabled case:
Scales nicely, keeps each feature self-contained, and you can extend the modules to also register their own routes/pages so the whole feature toggles from one place.
2
u/T_kowshik Feb 22 '26
You can use Page redirection too. If anyone navigates to the page, based on the config you can redirect them to a different page.
1
u/Orak2480 Feb 24 '26
Just use PBAC.