80

Would anyone know what I am missing, why those asp-controller and asp-action tags are not working for me. I am implementing a project in ASP.NET MVC Core.

This does not fire:

<a asp-controller="App" asp-action="Trips" class="btn btn-lg btn-success">Go to Trips</a>

Razor works fine:

@Html.ActionLink("Go to Trips", "Trips", "App", new object { }, new { @class = "btn btn-lg btn-success" })

Do I need to configure some service for that to work. And also, which way is preferred? Razor is pretty popular with MVC, are those asp- tags a new, better way?

1

8 Answers 8

165

After a little bit of digging I found that asp-controller and asp-action attributes are called anchor tag helpers, and are part of the

Microsoft.AspNetCore.Mvc.TagHelpers namespace

Apparently it is an alternative to using Razor. I was able to resolve the issue by creating '_ViewImports.cshtml' and adding the below into the file:

@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

Once done that, anchor tag helpers were recognized and button start working as anticipated.

7
  • 19
    Was creating my own areas when this problem appeared. I just copied the existing _ViewImports.cshtml and then it worked, thanks! Oct 20, 2017 at 23:17
  • awesome++. This is a very useful point to note especially when using areas.
    – Kurkula
    Mar 22, 2018 at 17:31
  • 3
    Because the scaffolding couldn't possibly actually include the required references...
    – Alexander
    May 22, 2018 at 18:38
  • 3
    Why asp attributes should be considered helpers when working in a ASP.NET Core MVC project? Code smell... but this answer solved my issue. Jan 10, 2019 at 6:12
  • 1
    I had this, except I didn't notice i terminated my line with a semi-colon. That killed the import helper. Once eliminated, it worked as expected. Does anyone know why that is? Is this the behavior of _ViewImports ? How would I have known if intellisense isn't warning me? Feb 3, 2020 at 22:15
16

When working with Areas I had to copy _ViewImport to the new Area\MyArea\Views

1
  • This should be a comment rather than an answer. You should post a full answer post with the description of how it solves the OP problem? Apr 11, 2020 at 9:46
11

Adding @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers to the top of the cshtml file is worked.

3

took me a while to figure it out -- but if you are like me still struggling with Cannot resolve symbol 'addTagHelper' after trying the accepted answer, you are probably running an ASP.NET MVC project instead of an ASP.NET Core project and therefore can not use the taghelper. Instead you have to use the htmlhelper

TLDR;

If this answer didn't work for you, please use the htmlhelper (*)

@using (Html.BeginForm("Trips", "App")) 
{
 ...
}

..instead of the taghelper (**)

@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers


<form asp-controller="App" asp-action="Trips">
   ...
</form>

(*) applies to

ASP.NET MVC 5.2

(**) applies to

ASP.NET Core 2.1, 1.0, 1.1, 2.0, 2.2, 3.0, 3.1, 5.0

2
@{    
  IDictionary<string, string> routeData = new Dictionary<string, string>();
  routeData.Add("Id", point.Id.ToString());
  routeData.Add("Name", point.Name);
  routeData.Add("Address", point.Address);
}
<a asp-page="./AddCollectionPoint" asp-page-handler="Edit" asp-all-route-data="@routeData">

Razor Pages have modelcshtml.cs files, inherited from "PageModel", SO In my case my action methods in those files were not being recognized...I replaced asp-controller with asp-page and asp-action with asp-page-handler. Plus added a line for default route in startup.cs.

   app.UseEndpoints(endpoints =>
   { endpoints.MapRazorPages(); endpoints.MapControllerRoute("default","{controller=Home}/{action=Index}");});

and it worked.

1
  • RouteData is helpful when you have a route like [Route("location/{id:int}/review")] and want Razor to generate a form with the id in the form's action
    – thran
    Sep 25, 2022 at 17:01
2

I had the same problem. Moreover manipulations with _ViewImports.cshtml didnt help. Taghelpers like this:

<a asp-controller="Contact" asp-action="GetContacts">Contact Form</a>

didn't navigate to controller method:

public class ContactController : Controller
{
    public IActionResult GetContacts()
    {
        return View();
    }
}

All I had to do is update a bit Startup.cs via MapControllerRoute method

app.UseEndpoints(endpoints =>
{
    endpoints.MapRazorPages();
    endpoints.MapControllerRoute(name:"default", pattern:"{controller}/{action}");
});
1

If you are using views within Areas, make sure you make a copy of the _ViewImports.cshtml file found in the /Views folder and copy it into your /Areas//Views folder too.

1
  • Note that, per the docs, rather than making a duplicate of the _ViewImports.cshtml file in each area's /Views folder, you can also just have a single _ViewImports.cshtml file in the project's root folder, which will then be shared by all views in the project, whichever area they belong to. Feb 16 at 21:14
0

In my case, none of this answer worked, so I just used the href tag and it's works.

1
  • InvalidOperationException: Cannot override the 'href' attribute for <a>. An <a> with a specified 'href' must not have attributes starting with 'asp-route-' or an 'asp-action', 'asp-controller', 'asp-area', 'asp-route', 'asp-protocol', 'asp-host', 'asp-fragment', 'asp-page' or 'asp-page-handler' attribute.
    – Jay
    Oct 24 at 21:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.