WebAPI found reference error when I have the assembly

C#asp.net Mvc-4asp.net Web-Api

C# Problem Overview


I've created a MVC 4 Web API Application inside my solution, but I'm getting 2 errors right now and I need some help.

> 'System.Web.Http.HttpConfiguration' does not contain a definition for > 'MapHttpAttributeRoutes' and no extension method > 'MapHttpAttributeRoutes' accepting a first argument of type > 'System.Web.Http.HttpConfiguration' could be found (are you missing a > using directive or an assembly reference?)

This error occurs on the following code

File: WebApiConfig.cs (at App_Start folder)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using System.Web;

    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services

            // Web API routes
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }
    }

Another one, is in the Global.asax

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

File: Global.asax.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Http.WebHost;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;

public class WebApiApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        GlobalConfiguration.Configure(WebApiConfig.Register);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }
}

enter image description here

UPDATE

After installing via Nuget the WebAPI WebHost (suggestion of @sa_ddam213) it resolves some problems, but now a got this error when run my application

> Could not load file or assembly 'System.Net.Http' or one of its > dependencies. The located assembly's manifest definition does not > match the assembly reference

Web.config file has the assembly

	<dependentAssembly>
		<assemblyIdentity name="System.Web.Http" publicKeyToken="31BF3856AD364E35" culture="neutral"/>
			<bindingRedirect oldVersion="0.0.0.0-5.2.0.0" newVersion="5.2.0.0"/>
	</dependentAssembly>

C# Solutions


Solution 1 - C#

A forced reinstall of WebAPI could do the job:

 update-package microsoft.aspnet.webapi -reinstall

Solution 2 - C#

I did:

get-project <project_name> | uninstall-Package Microsoft.AspNet.WebApi.WebHost -force

Then reinstalled (with specific version consistent with those in other projects)

get-project <project_name> | install-Package Microsoft.AspNet.WebApi.WebHost -Version 5.2.2

This solved the problem for me.

Solution 3 - C#

I uninstall some nuget packages in my project, including the MVC, and install all over again. Resolved. Thanks everybody for help me.

Solution 4 - C#

Update Your NuGet Packages. Worked for me.

Solution 5 - C#

Please try updating your packages by running the following command on nugetmanager console

update-package microsoft.aspnet.webapi.webhost -reinstall

Solution 6 - C#

Specifically for me, I downloaded from TFS someone at works solution and got the error

System.Web.Http.HttpConfiguration' does not contain a definition for 'MapHttpAttributeRoutes' and no extension method 'MapHttpAttributeRoutes' 

I was ABOUT TO go ahead with the re-install of web api, but I know past experiences that the dependency chain of the order in which you install items you can end up with a bit of drama.

JSON is hit or miss as sometimes I seen updated JSON force and uninstall of a newer version of another package and install an older version as it thought "it knew best.

For me UPDATE JSON via nuget was all i had to do.

Solution 7 - C#

  1. Check in your solution folder packages\Microsoft.AspNet.WebApi.Client.5.2.3\lib\net45 has System.Web.Http.dll is available or not. It should be there. If not available copy it from any other running project packages folder.
  2. Remove reference of System.Web.Http.dll from your project.
  3. Add new reference by pointing to new .dll that is to packages\Microsoft.AspNet.WebApi.Client.5.2.3\lib\net45\System.Web.Http.dll

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLucas_SantosView Question on Stackoverflow
Solution 1 - C#Dan OchianaView Answer on Stackoverflow
Solution 2 - C#garrypView Answer on Stackoverflow
Solution 3 - C#Lucas_SantosView Answer on Stackoverflow
Solution 4 - C#FAHIDView Answer on Stackoverflow
Solution 5 - C#Rajeev SirohiView Answer on Stackoverflow
Solution 6 - C#Tom StickelView Answer on Stackoverflow
Solution 7 - C#AtyulyaKadamView Answer on Stackoverflow