Encountered an unexpected error when attempting to resolve tag helper directive '@addTagHelper'

C#asp.net MvcRazorasp.net Mvc-5Visual Studio-2015

C# Problem Overview


I'm using Visual Studio 2015 Community edition, and I've created an ASP.NET MVC 5 project.

When I open a view (Index of Home or any other), it shows first three lines of the page underlined with red as a syntax issue. Here is the error:

> Encountered an unexpected error when attempting to resolve tag helper > directive '@addTagHelper' with value 'Microsoft.AspNet.Mvc.Razor.TagHelpers.UrlResolutionTagHelper, Microsoft.AspNet.Mvc.Razor'. > Error: Object reference not set to an instance of an object

The screenshot:

@addTagHelper error message

When I build the project, it build successfully. When I run it, it shows a lot of errors, but it runs the application.

Index View with errors

> The type or namespace name 'Mvc' does not exist in the namespace > 'Microsoft.AspNet' (are you missing an assembly reference?)

and

> '_Page_views_home_index_cshtml.ExecuteAsync()': no suitable method > found to override

How can I get rid of this?

C# Solutions


Solution 1 - C#

Here's how I fixed the issue:

First, reset the Visual Studio Component Cache by closing Visual Studio and deleting this folder: > C:\Users\[Username]\AppData\Local\Microsoft\VisualStudio\14.0\ComponentModelCache

And finally, check the web.config files and change:

<appSettings>
    <add key="webpages:Version" value="2.0.0.0" />
    ...
</appSettings>

to

<appSettings>
    <add key="webpages:Version" value="3.0.0.0" />
    ...
</appSettings>

Solution 2 - C#

I had the same problem, but the above didn't work. I also deleted all 4 files in the Component cache which didn't work. I noticed that the line below <appSettings> was set to false. I changed it to true and the red squigs were gone.

<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />

Solution 3 - C#

In my case it was the reference to System.Web.Mvc had disappeared.

There was lots of errors in the cshtml files but it would compile and run. Then I found that I had an error in the xxxcontrollers.cs file trying inherit controllers.

Seems it could compile because the dll was in bin already but it was giving errors.

I did not relish having to rebuild this project. That would be days and days.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiontarzanbappaView Question on Stackoverflow
Solution 1 - C#KcoderView Answer on Stackoverflow
Solution 2 - C#david thibodeauxView Answer on Stackoverflow
Solution 3 - C#BrownPonyView Answer on Stackoverflow