I am building a site using MVC4. I couldn't even begin that I am having a strange error. This is my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
namespace MyProject
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
}
The error message: The type or name of the assembly 'HttpConfiguration' does not exist in the namespace 'System.Web.Http' (are you missing a using directive or an assembly reference?)
It also doesn't recognize the readonly field RouteParameter. It occurs to me that it may have something to do with the version, my System.Web dll is version 4.0.0.0.
Another additional piece of information is that when I type "using System.Web. ", Intellisense DOES recognize the .Http dll
HOWEVER, in the code, it does not recognize any of its parameters.
I solved it. The issue was that, when I created the site, I updated all packages via Nuget. However, as I wasn´t going to use Entity Framework, I uninstalled it. In order to be able to uninstall that package, it required for me to uninstall Microsoft.AspNet.Providers.Core 1.2 as well... and I did.
This missing package messed things up. I cleared the project and started all over again. I could have also used the Update-Package command in the PM Console and would have restored all lost packages. However, since I had done so much mess compared to the little (next to null) work I had done, I decided to start it all over again.
Thanks anyway!
I encountered this error when cloning an MVC 4.0 application which previously worked for me. My problem was that the Microsoft.Net.Http
NuGet package wasn't restoring properly. I solved it from the NuGet console:
Update-Package -reinstall Microsoft.Net.Http
For others facing this issue, my solution was different. Firstly I was downgrading a project from .net 4.5 to .net 4.0, but the symptoms were the same as above:
The type or name of the assembly 'HttpConfiguration' does not exist in the namespace 'System.Web.Http' (are you missing a using directive or an assembly reference?)
I stumbled across a blog post which explained that there is a link between Newtonsoft.Json
and HttpConfiguration
, I removed the reference to Newtonsoft.Json
from the project and the packages.config
file then re-installed Newtonsoft.Json
from the package manager console:
Install-Package Newtonsoft.Json
I was not able to uninstall Newtonsoft.Json
from the package manage console due to other dependencies, hence the need to manually remove the reference.
Here's the link to the article: Newtonsoft.Json Hidden Dependencny on HttpConfiguration Breaks Compilation
1.In Solution Explorer, right-click the project node and click Add Reference.
2.In the Add Reference dialog box, select Assemblies tab. And type the “system.web.http” in the search box in the upper right corner.
3.Select the components you want to reference, if it is already selected than first remove the reference than **again Add Reference.
Rebuild the project again. Please let me know if it works. Thank you.
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