Cannot find AddMvc() in IServiceCollection in Visual Studio 2017

Viewed 19338

I have created a brand new asp.net core application. I have added the mvc Nuget package yet I am getting the below error.

Microsoft.AspNetCore.Mvc.Core 1.1.3

'IServiceCollection' does not contain a definition for 'AddMvc' and no extension method 'AddMvc' accepting a first argument of type 'IServiceCollection' could be found (are you missing a using directive or an assembly reference?)**

7 Answers

Use "Microsoft.AspNetCore.Mvc": "1.0.1" in dependencies section of Startup.cs in VS 2015 ASP.Net Core 1.0 project.

I think , AddMvc changed to AddMvcCore()

please add references

<PackageReference Include="Microsoft.AspNetCore" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.CookiePolicy" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.HttpsPolicy" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.2.0" />

in your projectname.csproj file

I just had the same error with visual studio 2017 when trying an 'Empty Project' (aspnet core 1.1 templates) template when creating an 'Asp.Net Core Web Application'.

Installing the latest 'Microsoft.AspNetCore.Mvc' on Nuget (version 2.2.0) failed with the message 'Package restore failed'. But installing a specific version (1.1.8) and reloading project (see Ahmed Al Jabri's answer above) fixed my error.

Related