[c#] GlobalConfiguration.Configure() not present after Web API 2 and .NET 4.5.1 migration

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.

This question is related to c# asp.net-web-api asp.net-mvc-5 asp.net-4.5

The answer is


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

using System.Web.Http;

this resolved the issue.


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


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.


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


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"


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


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


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;


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>

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

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.


Examples related to c#

How can I convert this one line of ActionScript to C#? Microsoft Advertising SDK doesn't deliverer ads How to use a global array in C#? How to correctly write async method? C# - insert values from file into two arrays Uploading into folder in FTP? Are these methods thread safe? dotnet ef not found in .NET Core 3 HTTP Error 500.30 - ANCM In-Process Start Failure Best way to "push" into C# array

Examples related to asp.net-web-api

Entity Framework Core: A second operation started on this context before a previous operation completed FromBody string parameter is giving null How to read request body in an asp.net core webapi controller? JWT authentication for ASP.NET Web API Token based authentication in Web API without any user interface Web API optional parameters How do I get the raw request body from the Request.Content object using .net 4 api endpoint How to use a client certificate to authenticate and authorize in a Web API HTTP 415 unsupported media type error when calling Web API 2 endpoint The CodeDom provider type "Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider" could not be located

Examples related to asp.net-mvc-5

'router-outlet' is not a known element The type or namespace name 'System' could not be found How to get 'System.Web.Http, Version=5.2.3.0? EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType How to extend available properties of User.Identity How to implement oauth2 server in ASP.NET MVC 5 and WEB API 2 How to get JSON object from Razor Model object in javascript The model backing the 'ApplicationDbContext' context has changed since the database was created How can I implement a theme from bootswatch or wrapbootstrap in an MVC 5 project? MVC 5 Access Claims Identity User Data

Examples related to asp.net-4.5

Register .NET Framework 4.5 in IIS 7.5 GlobalConfiguration.Configure() not present after Web API 2 and .NET 4.5.1 migration IIS_IUSRS and IUSR permissions in IIS8