Razor Pages Gotcha

I have been playing around with Razor Pages as of late, you can read more at https://docs.microsoft.com/en-us/aspnet/core/razor-pages/?view=aspnetcore-2.1&tabs=visual-studio.

The main difference with Razor Pages and ASP.NET MVC is that the Razor Page includes both the model and the controller. It is more of an MVVM approach, that also includes two-way data binding.

Anyway, I was struggling with the asp-page tag helper.

In my Details Razor Page I was using the asp-page tag helper to render links for my Edit Razor Page and Delete Razor Page.

<a asp-page="Edit" asp-route-id="Drill.Id" class="btn btn-primary">Edit</a>
<a asp-page="Delete" asp-route-id="Drill.Id" class="btn btn-link">Delete</a>
<a asp-page="Index" class="btn btn-link">Back to Drills</a>

To my surprise and frustration, my Edit and Delete links were NOT rendering at all, they were both just pointing to my Details Razor Page.

The trick was to remove the asp-route-id tag helper.

<a asp-page="Edit" class="btn btn-primary">Edit</a>
<a asp-page="Delete" class="btn btn-link">Delete</a>
<a asp-page="Index" class="btn btn-link">Back to Drills</a>

Once I made that change, the links to my Edit and Delete Razor Pages were created correctly. 

I used the asp-route-* tag helper in my ASP.NET MVC views with NO problems, and just assumed that was how it would work with Razor Pages.  It appears Razor Pages are a bit more opinionated and not as forgiving as ASP.NET MVC views, but nonetheless, as I work through the differences, I am enjoying Razor Pages more and more.

I won’t tell you how long it took me to figure this out, but hopefully it saves you an hour or two!

Leave a Reply

Your email address will not be published.