Could not load file or assembly 'System.Net.Http.Formatting' or one of its dependencies. The system cannot find the path specified

C#asp.net.Netasp.net Mvc-4

C# Problem Overview


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.

C# Solutions


Solution 1 - C#

Whenever I have a NuGet error such as these I usually take these steps:

  1. Go to the packages folder in the Windows Explorer and delete it.
  2. Open Visual Studio and Go to Tools > Library Package Manager > Package Manager Settings and under the Package Manager item on the left hand side there is a "Clear Package Cache" button. Click this button and make sure that the check box for "Allow NuGet to download missing packages during build" is checked.
  3. Clean the solution
  4. Then right click the solution in the Solution Explorer and enable NuGet Package Restore
  5. Build the solution
  6. Restart Visual Studio

Taking all of these steps almost always restores all the packages and dll's I need for my MVC program.


EDIT >>>

For Visual Studio 2013 and above, step 2) should read:

  1. Open Visual Studio and go to Tools > Options > NuGet Package Manager and on the right hand side there is a "Clear Package Cache button". Click this button and make sure that the check boxes for "Allow NuGet to download missing packages" and "Automatically check for missing packages during build in Visual Studio" are checked.

Solution 2 - C#

  • Remove all code references to System.Net.*

  • in the package window,

    Install-Package Microsoft.AspNet.WebApi.Client

  • Clean and rebuild your project

Solution 3 - C#

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.

Solution 4 - C#

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.

Solution 5 - C#

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>

Solution 6 - C#

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.

Solution 7 - C#

  1. Remove all code references to System.Net.*
  2. Uninstall: Package Microsoft.AspNet.WebApi and its dependencies.
  3. Reinstall all: Package Microsoft.AspNet.WebApi and its dependencies.
  4. Clean and rebuild your project

Solution 8 - C#

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

Solution 9 - C#

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.

Solution 10 - C#

For me it was as simple as

  1. Delete Microsoft.AspNet.WebApi.Client from the packages folder in Windows Explorer
  2. Open Tools > NuGet Package Manager > Package Manager Console
  3. Click the "Restore" button

Solution 11 - C#

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.

Solution 12 - C#

I was facing the same problem because

> System.Net.Http.Formatting

version written inside webconfig was 5.0.3 but inside the bin folder the library System.Net.Http.Formatting dll version was 4.0.2

so I just replaced with the same given inside bin

enter image description here

enter image description here

just do this clean project and build

Solution 13 - C#

For me this issue was caused by having a missing assembly binding entry in Web.config (System.Net.Http.Formatting) after adding the Microsoft.AspNet.WebApi.Client package.

I checked the Microsoft.AspNet.WebApi.Client version I had, and added the following binding to the Web.config:

  <dependentAssembly>
    <assemblyIdentity name="System.Net.Http.Formatting" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-5.2.7.0" newVersion="5.2.7.0" />
  </dependentAssembly>

Depending on the WebApi.Client version you have, change the version in the binding redirect.

Solution 14 - C#

In my case, none of the above worked, however, replacing 5.2.3.0 with 4.0.0.0 did solve the problem.

Solution 15 - C#

VS2019: 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.

Solution 16 - C#

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:

  1. Delete the faulty references in Project -> References (right click, ...)
  2. Build
  3. Navigate to the build errors "The type or namespace name [...] could not be found" or similar
  4. Use the "Show potential fixes" -> Install package

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>

Solution 17 - C#

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.

Solution 18 - C#

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;

Solution 19 - C#

What I did to solve this problem is

  1. Go to NuGet package manager.

  2. Select Updates (from the left panel)

  3. Update WebApi components

  4. After that, the project ran without errors.

Solution 20 - C#

Check your version matches both --> web.config file and --> Microsoft.AspNet.WebApi.Client,Microsoft.AspNet.WebApi if not pls update accourdingly

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNorrisView Question on Stackoverflow
Solution 1 - C#Jordan_WaltersView Answer on Stackoverflow
Solution 2 - C#user3919888View Answer on Stackoverflow
Solution 3 - C#realPTView Answer on Stackoverflow
Solution 4 - C#longestwayroundView Answer on Stackoverflow
Solution 5 - C#Mahmoud MoravejView Answer on Stackoverflow
Solution 6 - C#CularBytesView Answer on Stackoverflow
Solution 7 - C#SandeepView Answer on Stackoverflow
Solution 8 - C#bbodenmillerView Answer on Stackoverflow
Solution 9 - C#Hugo Nava KoppView Answer on Stackoverflow
Solution 10 - C#adam0101View Answer on Stackoverflow
Solution 11 - C#andreasnicoView Answer on Stackoverflow
Solution 12 - C#TAHA SULTAN TEMURIView Answer on Stackoverflow
Solution 13 - C#CoryView Answer on Stackoverflow
Solution 14 - C#Sebastián VansteenkisteView Answer on Stackoverflow
Solution 15 - C#grandholyView Answer on Stackoverflow
Solution 16 - C#Daniel HillebrandView Answer on Stackoverflow
Solution 17 - C#mowView Answer on Stackoverflow
Solution 18 - C#ashish VickyView Answer on Stackoverflow
Solution 19 - C#RajView Answer on Stackoverflow
Solution 20 - C#survesh m.View Answer on Stackoverflow