Search code examples
c#asp.net-coreasp.net-core-tag-helpers

TagHelpers Not Functioning in New Area


I created a new area on a project to separate concerns. However, I just ran into a strange issue where the taghelpers aren't working correctly, not even constructing the correct URL at all.

The original issue was on this:

<a class="btn btn-secondary btn-block font-weight-bold text-light" role="button" asp-area="Retail" asp-controller="RateQuery" asp-action="RateQuery"
   asp-route-partyid="@proOne.ProPartyId">
    @postFou.FouName<br>
    @postFou.FouAddr<br>
    @postFou.FouTown
</a>

I tried testing a bit:

<a href="/Retail/RateQuery/RateQuery?partyid=123">Test Link</a><br />
<a asp-controller="Main" asp-action="Index">Test Link to Home</a>

So, "Test Link" works, but "Test Link to Home" does not.

in the rest of my project, the regular area, everything works fine. I did register the area in the routing. Was I supposed to add something specific for taghelpers in the new area?

the project is on framework netcoreapp3.1

EDIT:

This also does not work:

<a asp-area="Retail" asp-controller="Main" asp-action="Index">Test Link to Home2</a>

Also, my Startup.cs:

 app.UseEndpoints(endpoints =>
{
    // Area route
    endpoints.MapControllerRoute(
        name: "areas",
        pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}");

endpoints.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapRazorPages();

            

            

Solution

  • First off, I want to credit this post: Using TagHelpers in Area Views

    The question is different, but the answer is in fact the same. You need to create a specific _ViewImport.cshtml file for each Area, in the Views folder.

    It appears that it can have the same markup as the main _ViewImports file, especially in my case where the _Layout file I'm using for these area views is located in the main trunk of the project.

    By adding this file, the taghelpers are functioning properly and rendering the correct url.