Could not load file or assembly ‘System.Web.WebPages.Razor, Version=3.1.1

asp.net-mvc-5entity-framework-6razor

I’m trying to upgrade a project from MVC 4/EF 5 to MVC 5/EF 6. I updated the framework from 4.5 to 4.5.1 and then from NuGet updated the Solution MVC (4.0.30506.0 to 5.1.1) and EntityFramework (5 to 6.1.1). This pulled in a Razor dependency (2.0.30506.0 to 3.1.1) and WebPages (2.0.30506.0 to 3.1.1). I edited the Project and Views folder web.config to reflect the changes and removed the GUID from the csproj.

When I run the site in debug I get an error loading the Views folder web.config on the —
section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection — line.

“An error occurred creating the configuration section handler for
system.web.webPages.razor/host: Could not load file or assembly
'System.Web.WebPages.Razor, Version=3.1.1”

It doesn’t have a problem on the previous line (System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup) or with the Razor reference in the root web.config. Unfortunately the stack trace doesn’t identify the calling assembly.

Calling assembly : (Unknown).

All the relevant files are included below.

Does anyone have any idea what the problem is and more importantly how to fix it. I only updated the bare minimum NuGet Packages: MVC and Entity Framework.

        packages.config:

…  
  <package id="EntityFramework" version="6.1.0" targetFramework="net451" />
  <package id="jQuery" version="2.0.2" targetFramework="net45" />
  <package id="jQuery.UI.Combined" version="1.10.3" targetFramework="net45" />
  <package id="jQuery.Validation" version="1.11.1" targetFramework="net45" />
  <package id="knockoutjs" version="2.2.1" targetFramework="net45" />
  <package id="Microsoft.AspNet.Mvc" version="5.1.1" targetFramework="net451" />
  <package id="Microsoft.AspNet.Mvc.FixedDisplayModes" version="5.0.0" targetFramework="net451" />
  <package id="Microsoft.AspNet.Razor" version="3.1.1" targetFramework="net451" />
  <package id="Microsoft.AspNet.Web.Optimization" version="1.0.0" targetFramework="net45" />
…
  <package id="Microsoft.AspNet.WebPages" version="3.1.1" targetFramework="net451" /> 
<!-- oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" -->
…

          web.config:       root

<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
…
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-3.1.1" newVersion="3.1.1" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-5.1.1" newVersion="5.1.1" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages.Razor" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.1.1" newVersion="3.1.1" />       </dependentAssembly>


          web.config:       Views folder

   <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.1.1, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
 <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.1.1, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
<section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.1.1, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />     </sectionGroup>
…
 <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.1.1, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
…
    <pages
        validateRequest="false"
        pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=5.1.1, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
        pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=5.1.1, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
        userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=5.1.1, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <controls>
        <add assembly="System.Web.Mvc, Version=5.1.1, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" />
      </controls>
    </pages>

          ERROR PAGE:

Configuration Error
Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately.

Parser Error Message: An error occurred creating the configuration section handler for system.web.webPages.razor/host: Could not load file or assembly 'System.Web.WebPages.Razor, Version=3.1.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)

Source Error:

Line 4:    <configSections>
Line 5:      <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.1.1, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
Line 6:        <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.1.1, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
Line 7:        <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.1.1, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
Line 8:      </sectionGroup>

Source File: C:\Project\views\web.config Line: 6

Assembly Load Trace: The following information can be helpful to determine why the assembly 'System.Web.WebPages.Razor, Version=3.1.1, Culture=neutral, PublicKeyToken=31bf3856ad364e35' could not be loaded.

=== Pre-bind state information ===
LOG: DisplayName = System.Web.WebPages.Razor, Version=3.1.1, Culture=neutral, PublicKeyToken=31bf3856ad364e35
(Partial)
WRN: Partial binding information was supplied for an assembly:
WRN: Assembly Name: System.Web.WebPages.Razor, Version=3.1.1, Culture=neutral, PublicKeyToken=31bf3856ad364e35 | Domain ID: 4
WRN: A partial bind occurs when only part of the assembly display name is provided.
WRN: This might result in the binder loading an incorrect assembly.
WRN: It is recommended to provide a fully specified textual identity for the assembly,
WRN: that consists of the simple name, version, culture, and public key token.
WRN: See whitepaper http://go.microsoft.com/fwlink/?LinkId=109270 for more information and common solutions to this issue.
LOG: Appbase = file:/// C:\Project /
LOG: Initial PrivatePath = C:\Project \bin

Calling assembly : (Unknown).

LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\Project \web.config
LOG: Using host configuration file: C:\Users\xxx\Documents\IISExpress\config\aspnet.config
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
LOG: Attempting download of new URL file:///C:/Users/xxx/AppData/Local/Temp/Temporary ASP.NET Files/root/898b17f9/848c1fdf/System.Web.WebPages.Razor.DLL.
LOG: Attempting download of new URL file:///C:/Users/xxx/AppData/Local/Temp/Temporary ASP.NET Files/root/898b17f9/848c1fdf/System.Web.WebPages.Razor/System.Web.WebPages.Razor.DLL.
LOG: Attempting download of new URL file:/// C:\Project /bin/System.Web.WebPages.Razor.DLL.
WRN: Comparing the assembly name resulted in the mismatch: Minor Version
ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated.

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18446

Best Answer

I was able to fix this by updating the configSection in the web.config of my views folder to match what I had in my packages.config. In my instance I needed to adjust the version for Razor to version 3.2.7

-- WEB.CONFIG

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

-- PACKAGES.CONFIG

<package id="Microsoft.AspNet.Mvc" version="5.2.7" targetFramework="net45" />
<package id="Microsoft.AspNet.Razor" version="3.2.7" targetFramework="net45" />