How do you mix Blazor components with traditional MVC controllers and Razor Pages within the same ASP.NET Core application?
Short answer
Since .NET 8's unified hosting model, a single ASP.NET Core project can register both MapControllers()/MapRazorPages() and MapRazorComponents () in the same Program.cs, letting traditional server-rendered pages and interactive Blazor components coexist -- useful for…
Advertisement
AModel answer
Since .NET 8's unified hosting model, a single ASP.NET Core project can register both MapControllers()/MapRazorPages() and MapRazorComponents<App>() in the same Program.cs, letting traditional server-rendered pages and interactive Blazor components coexist -- useful for incrementally adopting Blazor within an existing MVC application, or exposing both a JSON API (via controllers) and an interactive UI (via Blazor) from one codebase.
var app = builder.Build();
app.MapControllers(); // traditional REST API endpoints
app.MapRazorPages(); // traditional server-rendered pages
app.MapRazorComponents<App>() // Blazor interactive components
.AddInteractiveServerRenderMode();Real-world example
A team incrementally migrates a legacy MVC application to Blazor by adding new features as interactive Blazor components while leaving existing, stable MVC controllers and views entirely untouched within the same running application.
Likely follow-ups
- What routing conflicts can arise between MVC routes and Blazor component routes?
- How do you share layout/authentication state consistently across both UI paradigms?
Related topics