I have a small MVC app that I use for practice reasons, but now I am encountering an error every time I try to debug:
Could not load file or assembly 'System.Net.Http.Formatting' or one of its dependencies.
The system cannot find the path specified.
I've googled but cannot find a solution. I'm using .NET 4.5.
It can't be the DLL file because I'm using .Net 4.5.
This question is related to
c#
asp.net
.net
asp.net-mvc-4
System.Net.*
Microsoft.AspNet.WebApi
and its dependencies.Microsoft.AspNet.WebApi
and its dependencies. For me it was as simple as
Microsoft.AspNet.WebApi.Client
from the packages folder in Windows ExplorerVS2019: Tools -> Nuget Package Manager -> Package Manager Setting -> in Package Restore section, check 2 options. After that, go to project packages folder and delete all child folders inside (for no longer any error) Then Rebuild solution, Nuget will redownload all packages and project should run without any reference.
As originally suggested in a comment by GeoMac and documented on NuGet's docs the following worked for me when none of the other answers worked:
Tools / NuGet Package Manager / Package Manager Console
Update-Package -ProjectName MyProjectName -reinstall
For Me adding few below line in WebApi.config works as after updating the new nuget package did not works out
var setting = config.Formatters.JsonFormatter.SerializerSettings;
setting.ContractResolver = new CamelCasePropertyNamesContractResolver();
setting.Formatting = Formatting.Indented;
Don't forget to add namespace:
using Newtonsoft.Json.Serialization;
using Newtonsoft.Json;
user3919888 pointed me in the right direction, but I needed to run Update-Package -reinstall Microsoft.AspNet.WebApi.Client
in the Package-Manager console. Basic install by itself does not recognize the problem but does recognize that the package is already installed and does not overwrite it.
I'm posting this answer because this happens so infrequently that I end up googling and reaching this page before I remember what I did last time.
For those that use .NET Standard project in combination with .NET Framework projects:
In the .NET Standard way, packages that are included in a .NET Standard project will correctly be used across other .NET Core and .NET Standard projects.
Im the .NET Framework way, if you are referring to a .NET Standard project from an .NET Framework (MVC) project, you need to manually download and install the same nuget packages.
So the answer to my question was that I had to download and install Microsoft.AspNet.WebApi.Client
in the web project (.NET Framework) that is using a .NET Standard project where Microsoft.AspNet.WebApi.Client
is needed. In fact, I already had this installed but there was a version difference.
I just add this answer for others to see, it might not directly answer OP's question but it did save me time by first checking this instead of doing the top-voted answers.
What I did to solve this problem is
Go to NuGet package manager.
Select Updates (from the left panel)
Update WebApi components
After that, the project ran without errors.
What solved this annoying error for me was just to close Visual Studio and open it again. Then rebuild the solution, and it all worked again. Sorry for the crap answer, but I think it's worth an answer because it solved it for me.
In my case, none of the above worked, however, replacing 5.2.3.0 with 4.0.0.0 did solve the problem.
Removing the following lines from web.config
solved my problem. Note that in this project I didn't use WebApi components. So for others this solution may not work as expected.
<dependentAssembly>
<assemblyIdentity name="System.Net.Http.Formatting" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
</dependentAssembly>
I found an extra
<dependentAssembly>
<assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.2.28.0" newVersion="2.2.28.0" />
</dependentAssembly>
in my web.config. removed that to get it to work. some other package I installed, and then removed caused the issue.
I had the problem with a ASP.NET project in VS 2019.
Another symptom was, that some references (System.Web.Http) were marked as faulty in the project references list (Solution Explorer)
My solution:
The cause:
Looking at the difference in the csproj file I could see the reason for the trouble. Someone managed to reference a DLL in the Windows Program file folder!
<Reference Include="System.Web.Http">
<HintPath>..\..\..\..\..\..\Program Files (x86)\Microsoft ASP.NET\ASP.NET MVC 4\Packages\Microsoft.AspNet.WebApi.Core.4.0.30506.0\lib\net40\System.Web.Http.dll</HintPath>
</Reference>
<Reference Include="System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\..\..\..\Program Files (x86)\Microsoft ASP.NET\ASP.NET MVC 4\Packages\Microsoft.AspNet.Mvc.4.0.30506.0\lib\net40\System.Web.Mvc.dll</HintPath>
</Reference>
Remove all code references to System.Net.*
in the package window,
Install-Package Microsoft.AspNet.WebApi.Client
Clean and rebuild your project
Probably you need to set library reference as "Copy Local = True" on properties dialog. On visual studio click on "references" then right-click on the missing reference, from the context menu click properties, you should see copy local setting.
In my case none of the above solutions worked. I solved by right clicking on the reference
System.Net.Http.Formatting
from Visual studio and setting the property Copy Local to true.
I hope this is useful somehow.
Source: Stackoverflow.com