Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.Net Core: asp-action tag not working

Tags:

asp.net-core

I have this simple anchor tag.

<a asp-area="Admin" asp-action="Create" asp-controller="Users" class="btn btn-default">Create</a>

The code structure is as follows.

enter image description here

The markup that is generated is as follows

<a class="btn btn-default" href="/Admin/Users">Create</a>

It is missing the action (create) but the rest of the tags seem to be working fine.

Routing setup is as follows

app.UseMvc(
    routes =>
        {
            routes.MapRoute(name: "default", template: "{controller=Home}/{action=Index}/{id?}");
            routes.MapRoute(
                name: "areaRoute",
                template: "{area:exists}/controller=Admin/{action=Index}/{id?}");
        });
like image 224
Arsalan Avatar asked May 19 '17 19:05

Arsalan


2 Answers

The solution of this problem is to copy ~/Views/_ViewImports.cshtml to ~/AreaName/Views/_ViewImports.cshtml

This is because the Tag helper is made available because of @addTagHelper that is available in the _ViewImports.cshtml page.

Once you have done this, your Tag helper intellisense works as well as your Tag Helper code works!

Hope this helps. Thanks

Reference: [Resolved] Tag helper syntax is not working for the Areas view in ASP.NET Core - DotNetFunda.com

like image 122
Ali Jamal Avatar answered Nov 14 '22 05:11

Ali Jamal


Order matters... put the area route above that of the default.

          routes.MapRoute(
                name: "adminDefault",
                template: "{area:exists}/{controller=Home}/{action=Index}/{id?}");

           //catchall
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
like image 42
mvermef Avatar answered Nov 14 '22 05:11

mvermef