在Web API 2和.NET 4.5.1迁移之后,GlobalConfiguration.Configure()不存在

nzo*_*dlo 143 c# asp.net-4.5 asp.net-web-api asp.net-mvc-5

我最近开始按照本指南将我的项目迁移到.NET 4.5.1和Web Api 2.

MS开发人员Rick Anderson要求你做的第一件事就是改变:

WebApiConfig.Register(GlobalConfiguration.Configuration);
Run Code Online (Sandbox Code Playgroud)

GlobalConfiguration.Configure(WebApiConfig.Register);
Run Code Online (Sandbox Code Playgroud)

在global.asax文件中.然而,当我尝试构建时,这给了我一个错误:

错误1'System.Web.Http.GlobalConfiguration'不包含'Configure'的定义

我的项目目前是MVC 5和Web Api 2和.NET 4.5.1,但我认为System.Web.Http仍然认为它是.NEt 4.0版本.

我该怎么办呢?

谢谢.

编辑:

这是我的程序集绑定:

<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
  <dependentAssembly>
    <assemblyIdentity name="System.Net.Http.Formatting" publicKeyToken="31BF3856AD364E35" culture="neutral"/>
    <bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.0.0.0"/>
  </dependentAssembly>
  <!--
  <dependentAssembly>
    <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35"/>
    <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35"/>
    <bindingRedirect oldVersion="1.0.0.0-5.0.0.0" newVersion="5.0.0.0"/>
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35"/>
    <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35"/>
    <bindingRedirect oldVersion="1.0.0.0-5.0.0.0" newVersion="5.0.0.0"/>
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Web.WebPages.Razor" publicKeyToken="31bf3856ad364e35"/>
    <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
  </dependentAssembly> -->
</assemblyBinding>
Run Code Online (Sandbox Code Playgroud)

顶部评论后评论的所有内容都因为我收到了错误:

警告2发现无法解析的同一依赖程序集的不同版本之间发生冲突.当日志详细程度设置为详细时,这些引用冲突将在构建日志中列出.

并且摆脱硬绑定正在解决这个问题.

Tyr*_*ley 268

它需要system.web.http.webhost,它是此包的一部分.我通过安装以下包修复了这个问题:

PM> Install-Package Microsoft.AspNet.WebApi.WebHost 
Run Code Online (Sandbox Code Playgroud)

或者在nuget https://www.nuget.org/packages/Microsoft.AspNet.WebApi.WebHost/5.1.0中搜索它

  • 这对我不起作用.该软件包已经安装在我的项目中. (5认同)

Cha*_*rns 81

这些想法都没有帮助我的项目使用MVC 5.2.2.

  • System.web.Http 5.2.2已经安装
  • 删除Packages文件夹并完全重新下载所有NuGet库什么也没做
  • Web.config已经有System.Web.Http的dependentAssembly条目

强制重新安装纠正了问题.从NuGet包管理器控制台:

update-Package -reinstall Microsoft.AspNet.WebApi.WebHost

  • 我遇到了同样的问题,但重新安装并没有解决它,至少它看起来像那样,让我发疯,我不得不重新启动 VS2015,它就解决了。 (2认同)

Kir*_*lla 43

GlobalConfigurationclass是Microsoft.AspNet.WebApi.WebHostnuget包的一部分...您是否已将此包升级到Web API 2?


Tye*_*eth 28

除了使用包管理器控制台让nuget使用Install-Package Microsoft.AspNet.WebApi.WebHost更新项目以 缺少GlobalConfiguration,

我需要使用System.Web.Http的Install-Package Microsoft.AspNet.WebApi.SelfHost丢失;


Cam*_*ker 10

您可能想要检查您的项目是否已Microsoft.AspNet.WebApi.WebHost安装.事实证明,在我的情况下,Microsoft.AspNet.WebApi.WebHost安装在另一个项目中,但不是需要它的特定项目.在你的packages.config中,检查这样的行是否存在:

<package id="Microsoft.AspNet.WebApi.WebHost" version="5.1.1" targetFramework="net45" />
Run Code Online (Sandbox Code Playgroud)

如果不存在,则表示您尚未Microsoft.AspNet.WebApi.WebHost安装在项目中.您可以使用Nuget Package Manager或通过Package Manager控制台进行安装.要从程序包管理器控制台进行安装,请运行以下命令:

Install-Package Microsoft.AspNet.WebApi.WebHost
Run Code Online (Sandbox Code Playgroud)


小智 5

GlobalConfiguration.Configure API可在"Microsoft.AspNet.WebApi.WebHost"version ="5.2.3"中找到

而不是在"Microsoft.AspNet.WebApi.WebHost"version ="4.0.0"