asp-controller and asp-action attributes not working
Asked Answered
R

9

81

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?

Registered answered 5/8, 2016 at 20:32 Comment(1)
Con't call it MVC6, ASP.NET 5 is dead blogs.msdn.microsoft.com/webdev/2016/01/19/…Suspend
R
169

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.

Registered answered 5/8, 2016 at 21:6 Comment(7)
Was creating my own areas when this problem appeared. I just copied the existing _ViewImports.cshtml and then it worked, thanks!Thomasina
awesome++. This is a very useful point to note especially when using areas.Rein
Because the scaffolding couldn't possibly actually include the required references...Controvert
Why asp attributes should be considered helpers when working in a ASP.NET Core MVC project? Code smell... but this answer solved my issue.Incised
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?Retardment
In my case i changed from @addTagHelper *,Microsoft.AspNetCore.MVC.TagHelpers to @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers . I omitted the letter case.Obala
Thank man It's Working Fine, You Save my Lots of timeDirectional
K
16

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

Kinesiology answered 10/4, 2020 at 23:25 Comment(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?Kafir
E
11

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

Experimental answered 30/9, 2020 at 14:53 Comment(1)
This works, but you shouldn't need to rely on adding the line to each view (cshtml) that needs it - that's what the _ViewImports.cshtml file is for.Fluellen
M
4

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

Moseley answered 27/3, 2021 at 11:32 Comment(0)
M
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.

Marietta answered 28/9, 2020 at 2:40 Comment(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 actionSerpasil
S
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}");
});
Subdual answered 16/9, 2022 at 20:41 Comment(0)
A
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.

Adduction answered 30/7, 2022 at 11:8 Comment(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.Trimerous
S
0

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

Sholem answered 26/9, 2023 at 6:2 Comment(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.Holpen
F
0

If you're sure that you're using ASP.Net Core, the issue is likely that the folder containing your view doesn't have its own _ViewImports.cshtml file. In this case, go ahead and copy an existing one and then add the following line (if it doesn't already exist) to ensure TagHeaders are imported for all views contained within the folder:

@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

Fluellen answered 10/2 at 17:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.