C# – Problems with an MVC4 application trying to reference a newer version of System.Web.WebPages.Razor

asp.netasp.net-mvcasp.net-mvc-4crazor

I've been working on a mvc4 web application for the last few months. Everything was working great until friday when I ran an update-package in nuget to make sure that I had the most recent versions of my libraries.

Now when I try to view anything in the application it tells me that it get the error
Could not load file or assembly 'System.Web.WebPages.Razor, Version=3.0.0.0, 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)

I've checked my web.config and my packages.config and nowhere am I referencing System.Web.WebPages.Razor, Version=3.0.0.0

I've added a dependentAssembly block to my web.config with version 2.0.0.0

<dependentAssembly>
    <assemblyIdentity name="System.Web.WebPages.Razor" publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
</dependentAssembly>

and made sure that the web.config in my views directory also specifies v2.0.0.0

<sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
  <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
  <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
</sectionGroup>

I've also made sure that the version referenced in my references is 2.0.0.0. I've tried removing it and readding it. I've tried making sure that the server has the most recent version of the .net framework installed.

I've tested this both on the server and locally and get the same error message.

Locally I see this in the error log LOG: Post-policy reference: System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 But i can't find any information on what is causing that.

I'm developing it in Visual Studio 2012 premium.

Anyone have any ideas?

Best Answer

I've checked my web.config and my packages.config and nowhere am I referencing System.Web.WebPages.Razor, Version=3.0.0.0

That's your problem. I've ran into similar issues myself a few times. Though, it tends to happen mostly when dealing with multiple projects in one solution; I'm not sure if that applies in your case. Nevertheless, the issue boils down to the actual project reference having been "upgraded", but your web.config and packages.config not referencing that upgraded package. If you go into your project references and view properties on the Razor reference, I'd bet dollars to dimes that it says 3.0.0.0 there. You can either remove the reference and re-add it by browsing into the bin directory of the lower versioned package or upgrade it completely.

Like I said, this usually occurs in multi-project solutions when you upgrade one project but not another. The easiest way to fix it is to manage nuget packages for the entire solution. You'll then probably see multiple listings for Razor where one is checked for one or more projects but not some and vice-versa. Remove the older version of Razor (uncheck the associated projects) for the the older version and apply. Then go back to the new version of Razor and apply it to all projects it's missing from.