53

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>
7
  • are you sure you have System.Web in your projects "References" folder? Commented Aug 7, 2014 at 11:35
  • 2
    @JohanHjalmarsson Yes, I updated my question with a image where we can see some references that exist in this project Commented Aug 7, 2014 at 11:38
  • @Lucas_Santos can you check your web.config for some assembly redirects ?
    – nsgocev
    Commented Aug 7, 2014 at 11:41
  • 5
    have you installed Microsoft.AspNet.WebApi.WebHost pretty sure you can just nuget Install-Package Microsoft.AspNet.WebApi.WebHost
    – sa_ddam213
    Commented Aug 7, 2014 at 11:45
  • 1
    I uninstall some nuget packages in my project, including the MVC, and install all over again. Resolved. Thanks everybody for help me. Commented Aug 7, 2014 at 12:43

7 Answers 7

104

A forced reinstall of WebAPI could do the job:

 update-package microsoft.aspnet.webapi -reinstall
0
14

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.

7

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

7

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

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

Update Your NuGet Packages. Worked for me.

1

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

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.