Norris
Norris

Reputation: 2238

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

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.

Upvotes: 189

Views: 281693

Answers (22)

Anto Rex
Anto Rex

Reputation: 11

Thank you everyone for your answers, I too faced the same System.Net.dll error and scratching my head for 2 days , tried all your inputs but no use and the user Taha sultan who referred about the bin folder, got an idea to check my bin folder in the particular project

1.I checked my bin folder(showed an red * mark that System.Net.dll was ignored) under the particular project in the solution explorer as well as in the project folder in the system/laptop and found that the particular dll system.Net.dll is not available in the bin folder so I copied the same dll from the project root folder and pasted in the bin folder.

2.Clean the solution
3.Build the solution
4.Closed the solution
5.Reopened the solution and ran the application Hurray it got run .

All the very best to all

Upvotes: 1

Dave R
Dave R

Reputation: 1

One other thing to consider. When using .NET 5.0 apps (like blazor WASM) in a .NET 4.x solution, you may be adding conflicting files. Additionally, if you have installed Microsoft .NET SDK 6 or 7, it does not matter your target framework (such as 5.0). dotnet publish will always use the most recent version of the SDK. This can lead to unexpected problems because you now have incompatible dlls or even dlls you don't need. Try creating a global.json file to indicate the SDK you want to use during publish. https://learn.microsoft.com/en-us/dotnet/core/versions/selection

Upvotes: 0

survesh m.
survesh m.

Reputation: 1

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

Upvotes: 0

Cory
Cory

Reputation: 41

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.

Upvotes: 4

Daniel Hillebrand
Daniel Hillebrand

Reputation: 681

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>

Upvotes: 1

grandholy
grandholy

Reputation: 11

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.

Upvotes: 1

Jordan_Walters
Jordan_Walters

Reputation: 2221

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.

Upvotes: 203

Sandeep
Sandeep

Reputation: 967

  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

Upvotes: 6

CularBytes
CularBytes

Reputation: 10321

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.

Upvotes: 14

Raj
Raj

Reputation: 160

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.

Upvotes: 1

TAHA SULTAN TEMURI
TAHA SULTAN TEMURI

Reputation: 5241

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

Upvotes: 2

ashish Vicky
ashish Vicky

Reputation: 11

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;

Upvotes: 0

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

Upvotes: 1

Hugo Nava Kopp
Hugo Nava Kopp

Reputation: 3064

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.

Upvotes: 3

andreasnico
andreasnico

Reputation: 1488

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.

Upvotes: 2

adam0101
adam0101

Reputation: 31055

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

Upvotes: 2

bbodenmiller
bbodenmiller

Reputation: 3181

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

Upvotes: 8

longestwayround
longestwayround

Reputation: 1019

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.

Upvotes: 24

mow
mow

Reputation: 301

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.

Upvotes: 0

Mahmoud Moravej
Mahmoud Moravej

Reputation: 9044

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>

Upvotes: 17

realPT
realPT

Reputation: 1125

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.

Upvotes: 29

user3919888
user3919888

Reputation: 2029

  • Remove all code references to System.Net.*

  • in the package window,

    Install-Package Microsoft.AspNet.WebApi.Client

  • Clean and rebuild your project

Upvotes: 202

Related Questions