birdus
birdus

Reputation: 7504

Why isn't .NET looking in my output path for any binaries?

I've inherited a Visual Studio web project and the Output Path is specified as objd\amd64\. When I build, all the files (both compiled project files along with packages, such as MVC) show up there. However, when I hit F5 to run the app, I get the following exception:

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 exception goes on to list places where the system is looking for that (and other) files. The first place it looks is bin, which is the normal output folder. Why isn't .NET looking in the specified output folder, and how can I make it look there?

CLARIFICATION: Just to be clear, this exception is not unique to MVC. If I manually create the bin folder and copy the MVC DLL there, then I get past this exception, but get another one for the next file it looks for. If I copy all the files to bin, then the app runs fine. The runtime just isn't looking in the specified output folder for the files.

UPDATE: If I change the Output Path to bin, NO files are output there. There is no post-build event defined that I can see.

UPDATE: Here are my build settings. The bin folder isn't even being created when I build the project (I'm trying to use bin for test purposes), let alone getting files copied into it. Debug is definitely the selected build type when I hit F5.

enter image description here

UPDATE: Here are the Solution config settings: enter image description here

Upvotes: 0

Views: 758

Answers (2)

NightOwl888
NightOwl888

Reputation: 56909

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:

  1. Mismatching MVC (and/or MVC dependency) version numbers between 2 different .csproj files in your solution.
  2. Mismatching MVC (and/or MVC dependency) version numbers between your .csproj file and web.config and/or /Views/web.config.
  3. One or more .csproj files has an invalid <HintPath> for an MVC (and/or MVC dependency) DLL reference location.
  4. 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.

  1. Right-click on your project node in Solution Explorer and click Unload Project.
  2. Right-click the project node again and click Edit .csproj.
  3. 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.
  4. 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.

Upvotes: 1

Majid Parvin
Majid Parvin

Reputation: 5062

Update the Application web.config File

Be sure to make these changes in the app web.config file, not the web.config file in the Views folder.

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

Upvotes: 0

Related Questions