Arsalan
Arsalan

Reputation: 151

ASP.Net Core: asp-action tag not working

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?}");
        });

Upvotes: 5

Views: 9749

Answers (3)

Ali reza Soleimani Asl
Ali reza Soleimani Asl

Reputation: 167

If you have Area section in your project just copy _ViewImports.cshtml from your Views in the root folder to Areas>Admin>Views folder. Be sure that this code be there in _Viewimports.cshtml

@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

Upvotes: 0

Ali Jamal
Ali Jamal

Reputation: 1559

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

Upvotes: 8

mvermef
mvermef

Reputation: 3914

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?}");

Upvotes: 3

Related Questions