Enabling Blazor-Enhanced Navigation in an ASP.NET Core MVC App

Author Lee Timmins Date 2nd Oct 2025 Comments Comments

If you want to enable Blazor-enhanced navigation in your ASP.NET Core MVC or Razor Pages app, there are a few setup steps required. This allows navigation between pages to feel smoother, similar to a single-page app, without unnecessary full page reloads. Here’s how to get it working.

Step 1. Update Program.cs

First, you need to enable controllers with views and map your Razor components. In your Program.cs, add:

builder.Services.AddControllersWithViews();

Place this line before:

var app = builder.Build();

Then, right before app.Run(), add:

app.MapRazorComponents<App>();

Where App is a simple class defined as:

public class App { }

Step 2. Add Blazor Script to _Layout.cshtml

Next, add the Blazor client-side script so navigation can be enhanced. In your _Layout.cshtml file, just before the closing </body> tag, insert:

<script src="~/_framework/blazor.web.js"></script>

Step 3. Configure Response Headers

To let Blazor-enhanced navigation work, your endpoints must explicitly allow it by returning a special header. You can achieve this by overriding OnActionExecuting in your controllers:

public override void OnActionExecuting(ActionExecutingContext context)
{
    Response.Headers.Append("blazor-enhanced-nav", "allow");
    base.OnActionExecuting(context);
}

This ensures every response contains the required header.

Step 4. Fix Navigation Back to Home

At this point, enhanced navigation will work when moving between pages in the same directory. However, you might notice that navigating back to the home page still triggers a full reload.

To fix this, add a <base> tag to your layout’s <head> section:

<base href="/" />

This ensures Blazor can correctly resolve routes when navigating back to root-level endpoints.

Final Thoughts

With these steps, you’ll have Blazor-enhanced navigation fully working in your ASP.NET Core app. The experience will feel faster and smoother, giving your app more of an SPA feel without requiring a full Blazor app.