Tania Marinova
Tania Marinova

Reputation: 1908

Exception – Could not load file or assembly 'System.Web.Mvc, Version=4.0.0.1…'

There is a problem – only when I request one single controller – I get the exception:

Could not load file or assembly 'System.Web.Mvc, Version=4.0.0.1, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040

It's strange because in my config file I see that the version is 4.0.0.0 and and all the other controllers are fine and only when I upload the application on the server this thing happens.

Upvotes: 16

Views: 50209

Answers (5)

Neo
Neo

Reputation: 4497

I got this also at run-time, and it was due to my project (a unit test project using NuGet PackageReference) referencing an ASP.NET website project that already had the necessary binding redirects in place (as suggested by the compiler warnings when building). I had to add the same binding redirect in the unit test project.

<runtime>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <dependentAssembly>
      <assemblyIdentity name="System.Web.Mvc" culture="neutral" publicKeyToken="31bf3856ad364e35" />
      <bindingRedirect oldVersion="0.0.0.0-5.2.6.0" newVersion="5.2.6.0" />
    </dependentAssembly>
  </assemblyBinding>
</runtime>

Upvotes: 0

C&#233;sar Le&#243;n
C&#233;sar Le&#243;n

Reputation: 3060

Go to Nuget manager, search for: "Microsoft.AspNet.Mvc", and press update button.

That work on my case.

Upvotes: 0

Tomas Kubes
Tomas Kubes

Reputation: 25168

You maybe can reference ASP.NET MVC as reference to GAC, but it can happens that different developers have different version of MVC installed.

Remove references to ASP:NET MVC and add ASP.NET MVC as nuget package.

Upvotes: 0

Andre Luus
Andre Luus

Reputation: 3812

I got this build error on a VSO agent thanks to the ASP.Net assembly resolve weirdness. I had changed some of my build configurations and Visual Studio modified the output directory for the MVC project to \bin\x86\Debug or something similar.

This had bitten me before and I remembered ASP.Net wants to be built to \bin ONLY.

So when you get any kind of "reference could not be found" on build time, remember to check that your build output directory is only \bin:

enter image description here

More on the problem here: Changing output directory for asp.net project in Visual Studio

Hope this helps someone.

Upvotes: 1

staticvoidmain
staticvoidmain

Reputation: 793

This is a known issue. Seems you have migrated recently from 3.0 to 4.0 or this application is a fresh clone from the repository. The one single controller may not have nuget restored. I have also faced this issue. I got this msdn link and browsing it had a solution. Here are the steps mentioned.

The problem can be resolved by implementing one of the following solutions:

  1. (Preferred) Install Microsoft.AspNet.Mvc from the NuGet gallery (this will install a binding redirect in your web.config). You can do this from the NuGet package manager or the NuGet console inside Visual Studio:

    Install-Package Microsoft.AspNet.Mvc -Version -Project PROJECTNAME

    MVC 4 version: 4.0.40804.0

    MVC 3 version: 3.0.50813.1

  2. Manually update the reference to System.Web.MVC.dll (don’t use the one in the GAC).

    Try the Add Reference -> Assemblies -> Extensions dialog box.

In either case ensure that the Copy Local project property for the assembly is set to true so it ends up in your bin folder which is needed for deployment. There is a known NuGet bug that resets the Copy Local flag: https://nuget.codeplex.com/workitem/4344

Install Nuget Package Microsoft.AspNet.Mvc for all the project referencing System.Web.Mvc dll Hope this solves your problem

Upvotes: 23

Related Questions