150

I recently started following this guide to migrate my project to .NET 4.5.1 and Web Api 2.

The very first thing MS developer Rick Anderson asks you to do is change:

WebApiConfig.Register(GlobalConfiguration.Configuration);

to

GlobalConfiguration.Configure(WebApiConfig.Register);

in the global.asax file. Yet this is giving me an error when I try to build:

Error 1 'System.Web.Http.GlobalConfiguration' does not contain a definition for 'Configure'

My project is currently on MVC 5 and Web Api 2 and .NET 4.5.1, yet I think System.Web.Http still thinks it's the .NEt 4.0 version.

How can I go about fixing this?

Thank you.

Edit:

Here are my assembly bindings:

<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>

Everything commented out after the top was commented because I was getting the error:

Warning 2 Found conflicts between different versions of the same dependent assembly that could not be resolved. These reference conflicts are listed in the build log when log verbosity is set to detailed.

and getting rid of the hard bindings was fixing that.

Erik Philips
  • 48,663
  • 7
  • 112
  • 142
nzondlo
  • 4,024
  • 4
  • 16
  • 20

11 Answers11

276

It needs the system.web.http.webhost which is part of this package. I fixed this by installing the following package:

PM> Install-Package Microsoft.AspNet.WebApi.WebHost 

or search for it in nuget https://www.nuget.org/packages/Microsoft.AspNet.WebApi.WebHost/5.1.0

abatishchev
  • 92,232
  • 78
  • 284
  • 421
Tyrone Moodley
  • 3,162
  • 1
  • 13
  • 14
  • Was added automatically when I added a API controller. I had to restart VS2015 to get it going. I also did everything else recommended on this page so not sure what was the actual problem. – Piotr Kula May 01 '15 at 09:38
  • 6
    That didn't work for me. The package is already installed in my project. – Shimmy Weitzhandler Dec 09 '15 at 11:58
  • Interesting was that the webhost was already installed and still was not working, then I change the order in which the `GlobalConfiguration.Configure(WebApiConfig.Register);` was inserted in the global.asax to the top just after the AreaRegistration, and It worked just fine. – PedroSouki Jun 23 '16 at 14:33
  • why they did not wrote something like it? it takes half an hour for me to find your answer – Homayoun Behzadian Jul 25 '18 at 07:21
  • Worked for me after updating `Microsoft.AspNet.WebApi.WebHost` to the latest version – Peter van Kekem Apr 18 '19 at 11:59
  • I also had to add `using System.Web.Http;` to the Global.asax.cs file – vbguyny Oct 02 '20 at 02:56
86

None of these ideas helped my project using MVC 5.2.2.

  • System.web.Http 5.2.2 was already installed
  • Deleting the Packages folder and completely re-downloading all NuGet libraries did nothing
  • Web.config already had a dependentAssembly entry for System.Web.Http

Forcing a reinstall corrected the problem. From the NuGet package manager console:

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

Charles Burns
  • 9,631
  • 7
  • 58
  • 77
  • 1
    I had the same problem but reinstalling didn't fix it, least it looked like that and was driving me nuts, I had to restart VS2015 and it sorted out. – Piotr Kula May 01 '15 at 09:37
  • Looks like the combination of restart vs2013 + reinstall resolved the problem for me – Ram Mar 12 '16 at 08:38
44

GlobalConfiguration class is part of Microsoft.AspNet.WebApi.WebHost nuget package...Have you upgraded this package to Web API 2?

Kiran Challa
  • 53,599
  • 15
  • 167
  • 151
30

As well as using Package manager console to get nuget to update the project with Install-Package Microsoft.AspNet.WebApi.WebHost for missing GlobalConfiguration,

I needed Install-Package Microsoft.AspNet.WebApi.SelfHost for missing using System.Web.Http;

Tyeth
  • 673
  • 5
  • 12
10

You may want to check that your project has Microsoft.AspNet.WebApi.WebHost installed. As it turns out, in my case, Microsoft.AspNet.WebApi.WebHost was installed in another project, but not the particular project that needed it. In your packages.config, check that a line like this is there:

<package id="Microsoft.AspNet.WebApi.WebHost" version="5.1.1" targetFramework="net45" />

If that is not present, you don't have Microsoft.AspNet.WebApi.WebHost installed in your project. You can either install using Nuget Package Manager or through the Package Manager Console. To install from the Package Manager Console, run this command:

Install-Package Microsoft.AspNet.WebApi.WebHost
Cameron Tinker
  • 9,119
  • 10
  • 42
  • 80
6

GlobalConfiguration.Configure API is available in "Microsoft.AspNet.WebApi.WebHost" version="5.2.3"

and not in "Microsoft.AspNet.WebApi.WebHost" version="4.0.0"

user3508883
  • 61
  • 1
  • 1
3

If the issue remain after uninstalling and installing Microsoft.AspNet.WebApi.WebHost then also add followings in web.config for globalconfiguration to work

 <dependentAssembly>
        <assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.2.0.0" />
      </dependentAssembly>
2

"Install-Package Microsoft.AspNet.WebApi.Core" worked just fine.

1

My problem was that the nuget package manager did not do a real update but the files contained references to older versions, so i first removed all the installed mvc related packages (including razor and web api) then installed them over again and updating the version of the razor in views/web.config to version 3.0.0.

Soniku
  • 124
  • 1
  • 4
1

None of these solutions worked for me. I had a tangle of Nuget packages that couldn't update because of circular dependencies on each other.

I would up having to fix this the old-fashioned way. I created a new MVC/web api project and manually copied System.Web.Http and System.Web.Http.WebHost from the new project into the Nuget folders of the exisitng solution. From there I updated the references by, OMG, "browsing" and fixed the problem.

Micah B.
  • 958
  • 2
  • 11
  • 25
0

this resolved this issue by adding namespace to Global.asax.cs file.

using System.Web.Http;

this resolved the issue.