简体   繁体   English

在 Visual Studio 2017 的 IServiceCollection 中找不到 AddMvc()

[英]Cannot find AddMvc() in IServiceCollection in Visual Studio 2017

I have created a brand new asp.net core application.我创建了一个全新的 asp.net 核心应用程序。 I have added the mvc Nuget package yet I am getting the below error.我已经添加了 mvc Nuget 包,但出现以下错误。

Microsoft.AspNetCore.Mvc.Core 1.1.3 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?)** “IServiceCollection”不包含“AddMvc”的定义,并且找不到接受“IServiceCollection”类型的第一个参数的扩展方法“AddMvc”(您是否缺少 using 指令或程序集引用?)**

Install-Package Microsoft.AspNetCore.Mvc -Version 1.1.3安装包 Microsoft.AspNetCore.Mvc -Version 1.1.3

--Update - 更新

Unload the project and Reload it again (restart)卸载项目并再次重新加载(重新启动)

Try restarting visual studio 2017.尝试重新启动 Visual Studio 2017。

Sounds dumb I know, but it worked for me.我知道这听起来很愚蠢,但它对我有用。 I added Microsoft.AspNetCore.Mvc, but Visual Studio still told me that "IServiceCollection does not contain a definition for AddMvc ...".我添加了 Microsoft.AspNetCore.Mvc,但 Visual Studio 仍然告诉我“IServiceCollection 不包含 AddMvc 的定义......”。 I shutdown and restarted visual studio 2017 and the error message went away.我关闭并重新启动了 Visual Studio 2017,错误消息消失了。 Visual Studio 2017 is new and still seems to be a bit flaky, particularly with packages. Visual Studio 2017 是新的,但似乎仍然有点不稳定,尤其是在包方面。 Hopefully this will get fixed in a future update.希望这会在未来的更新中得到修复。 Right Microsoft?对微软?

Uninstall any version of MVC from "Manage NuGet Package" window.从“管理 NuGet 包”窗口卸载任何版本的 MVC。 Add this <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.3" /> to "yourprojectname.csproj" file manually.将此<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.3" />手动添加到“yourprojectname.csproj”文件。 It seems the problem is related to the version of the package.似乎问题与包的版本有关。

在 VS 2015 ASP.Net Core 1.0 项目的Startup.cs dependencies部分中使用"Microsoft.AspNetCore.Mvc": "1.0.1"

我认为,AddMvc 改为 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在您的projectname.csproj文件中

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'.在创建“Asp.Net Core Web 应用程序”时尝试使用“空项目”(aspnet 核心 1.1 模板)模板时,我在 Visual Studio 2017 中遇到了同样的错误。

Installing the latest 'Microsoft.AspNetCore.Mvc' on Nuget (version 2.2.0) failed with the message 'Package restore failed'.在 Nuget(2.2.0 版)上安装最新的“Microsoft.AspNetCore.Mvc”失败,并显示消息“包恢复失败”。 But installing a specific version (1.1.8) and reloading project (see Ahmed Al Jabri's answer above) fixed my error.但是安装特定版本 (1.1.8) 并重新加载项目(参见上面 Ahmed Al Jabri 的回答)修复了我的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM