Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebAPI found reference error when I have the assembly

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>
like image 874
Lucas_Santos Avatar asked Aug 07 '14 11:08

Lucas_Santos


5 Answers

A forced reinstall of WebAPI could do the job:

 update-package microsoft.aspnet.webapi -reinstall
like image 171
Dan Ochiana Avatar answered Nov 13 '22 08:11

Dan Ochiana


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.

like image 26
garryp Avatar answered Nov 13 '22 08:11

garryp


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

like image 6
Lucas_Santos Avatar answered Nov 13 '22 08:11

Lucas_Santos


Update Your NuGet Packages. Worked for me.

like image 6
FAHID Avatar answered Nov 13 '22 08:11

FAHID


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

update-package microsoft.aspnet.webapi.webhost -reinstall
like image 6
Rajeev Sirohi Avatar answered Nov 13 '22 08:11

Rajeev Sirohi