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);
}
}
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>
A forced reinstall of WebAPI could do the job:
update-package microsoft.aspnet.webapi -reinstall
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.
I uninstall some nuget packages in my project, including the MVC, and install all over again. Resolved. Thanks everybody for help me.
Update Your NuGet Packages. Worked for me.
Please try updating your packages by running the following command on nugetmanager
console
update-package microsoft.aspnet.webapi.webhost -reinstall
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With