Could not load file or assembly 'System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.
The error is a compile error. This means the build is crashing before the MVC runtime is launched.
This is easy to prove. Delete the bin folder and try to compile again. If it is not re-created and/or there are a lot of files missing, it means the build is not making it all the way through.
Some things that could be causing this are:
- Mismatching MVC (and/or MVC dependency) version numbers between 2 different
.csproj files in your solution.
- Mismatching MVC (and/or MVC dependency) version numbers between your
.csproj file and web.config and/or /Views/web.config.
- One or more
.csproj files has an invalid <HintPath> for an MVC (and/or MVC dependency) DLL reference location.
- NuGet package restore is not set up at all or is not set up properly. The easy way to check this is to look in your
packages/ folder to see if the reference in the error message exists on disk.
The best (safest) way to solve this is to go through these files manually. Visual Studio doesn't always make the right decisions when upgrading dependency versions or changing file locations.
Typical MVC 5 references and versions should look like this in the .csproj file (you may need to adjust the version numbers and net45x version in the <HintPath> accordingly):
<Reference Include="System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<Private>True</Private>
<HintPath>..\packages\Microsoft.AspNet.Mvc.5.0.0\lib\net45\System.Web.Mvc.dll</HintPath>
</Reference>
<Reference Include="System.Web.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<Private>True</Private>
<HintPath>..\packages\Microsoft.AspNet.Razor.3.0.0\lib\net45\System.Web.Razor.dll</HintPath>
</Reference>
<Reference Include="System.Web.WebPages, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<Private>True</Private>
<HintPath>..\packages\Microsoft.AspNet.WebPages.3.0.0\lib\net45\System.Web.WebPages.dll</HintPath>
</Reference>
<Reference Include="System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<Private>True</Private>
<HintPath>..\packages\Microsoft.AspNet.WebPages.3.0.0\lib\net45\System.Web.WebPages.Razor.dll</HintPath>
</Reference>
Update
I see you have posted Visual Studio's project properties. The configuration is highly customized and I don't recommend you use Visual Studio's project property designer to edit it, because it could cause the configuration to become corrupted.
My suggestion is to use the following procedure to review your .csproj file references. This is much safer than deleting references and re-adding them, since Visual Studio doesn't support a way to edit everything in the .csproj file that could be there.
- Right-click on your project node in Solution Explorer and click Unload Project.
- Right-click the project node again and click Edit .csproj.
- Search the file for references to each of the above assemblies and update the version and the HintPath accordingly. Make sure the HintPath you use actually points to an existing file on disk.
- Repeat these steps for all dependent projects in the solution (and any that are in DLLs that are not part of the solution).
You should also review the assembly versions (especially those of MVC) in your web.config and Views/web.config and update them, if necessary. See the link for more detailed information.