ASP .Net Core Razor SPA vs. MVC Taghelpers Navigation
As I mess about with deciding- do I want to do Razor SPA pages or do a full on ASP.Core MVC pages for my latest Web App? I noticed something when- Yes I put both in a solution- it comes to the ASP tag helper for navigation links…
So here’s the thing. For a regular ASP.Core MVC we see tag helper navigations look like this:
<a class="nav-link" asp-controller="Home" asp-action="Index">Home</span></a>
Simple enough… “asp-controller” has the simple controller name, and “asp-action” the action within the controller… no problem
Now consider Razor SPA or just simple razor pages:
<a class="nav-link text-dark btn btn-outline-info" asp-page="/Index" data-toggle="tooltip" data-placement="top" title="Always Takes you back Home">Home</a>
Here we have a “asp-page”. Ok, I do get it… Razor SPA or Razor Pages do not follow the MVC pattern with a controller etc. But why do they need to be different? Yes I understand MVC came first and it is very understandable to have a clear navigation tag helper. But again, why not just one simple tag helper that sorts out where the page and/or controller is?
This gets even more complex – mostly- when one considers “Areas”.
Again very well defined for an MVC pattern. But to use within Razor Pages, you have to muck about with making sure this is available in your Startup.ConfigureServices. Why oh WHY?
services.AddMvc() .AddRazorPagesOptions(options => options.AllowAreas = true);
Maybe some day this will evaporate, but for now… no.
fyi… as this applies to ASP.Net Core 2.2, the doc reference is here:
Anchor Tag Helper in ASP.NET Core
Anchor Tag Helper in ASP.NET Core
Discover the ASP.NET Core Anchor Tag Helper attributes and the role each attribute plays in extending behavior of the HTML anchor tag.
Happy tagging,
~SG