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

12

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

razor
asp.net-mvc-5
entity-framework-6
asked on Stack Overflow Apr 2, 2014 by Joe

5 Answers

5

Decided it wasn’t worth it trying to figure out these nightmarish dependencies. I restored a backup of the Project and from the Package Manager Console (PMC) manually updated MVC and EntityFramework with older versions that didn’t call in the Razor 3.1.1 dependency.

Install-Package Microsoft.AspNet.Mvc -Version 5.0.0

Install-Package entityframework -Version 6.0.0

Note: If you use the DbContext.EntityState method it has been moved from System.Data to System.Data.Entity, update your usings as appropriate.

If you have Simple membership you will need to install Microsoft.AspNet.WebPages.WebData (see Is ASP.NET MVC 5 incompatible with the WebMatrix SimpleMembershipProvider? ) and if you have OAuth in the Project (The MVC4 template installs OAuth into the project, I wasn’t using it so I was able to remove it. If you are using it you have more work cut out for you) you’ll have to Uninstall OAUTH (WebData had a dependency issue with OAUTH).

From NuGet Uninstall Microsoft WebPages OAuth library

From PMC Install-Package Microsoft.AspNet.WebPages.WebData

Be sure to follow these steps as appropriate to your Project: Upgrade MVC 4 to 5 http://www.asp.net/mvc/tutorials/mvc-5/how-to-upgrade-an-aspnet-mvc-4-and-web-api-project-to-aspnet-mvc-5-and-web-api-2

-- OR --

If you want to try Updating all your packages follow these steps:

From NuGet Uninstall Microsoft WebPages OAuth library (if using SimpleMembership, see above)

Had to individually update: 'Microsoft ASP.NET Web API Client Libraries and Core Libraries' and 'System.Spatial for OData'

Update All

From PMC Install-Package Microsoft.AspNet.WebPages.WebData (SimpleMembership, see above)

(See DbContext.EntityState above)

(web.config-root, add key="webpages:Version" value="3.0.0.0"; see Upgrade MVC 4 to 5 above; other changes were made automatically)

(Views web.config requires manual changes)

(Was then able to successfully Build and Open project but when it accessed the DB it required an update. (From PMC Update-Database. It failed with on “CREATE INDEX [xxxxxxx] ON [dbo].xxxxxxxx” “System.Data.SqlClient.SqlException (0x80131904): The operation failed because an index or statistics with name 'xxxxxxxx' already exists on table 'dbo.xxxxxxx'.”) but I was then able to successfully access the DB)

answered on Stack Overflow Apr 2, 2014 by Joe • edited May 23, 2017 by Community
4

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" />
answered on Stack Overflow Jan 29, 2019 by TroySteven
0

Had similar problem with System.Web.Mvc when I copied my project over to a Windows 8 machine from a Windows 7, and did update-packages, solved it by removing the Reference to System.Web.Mvc and adding it back by browsing to version 5.2 which I found in:

C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET Web Stack 5\Packages\Microsoft.AspNet.Mvc.5.2.0\lib\net45

It was also in: C:(my solution folder)\packages\Microsoft.AspNet.Mvc.5.2.3\lib\net45

Click by click detail:

Solved it by going to Solution Explorer window in Visual Studio 2013 Express, then References, right clicking on System.Web.Mvc and remove. Then I added the reference back by right clicking on References and "Add Reference...", But, I didn't add it the normal way of selecting Assemblies, I clicked browse and found version 5.2

answered on Stack Overflow Feb 21, 2015 by rzor1861330 • edited Feb 22, 2015 by rzor1861330
0

I had similar problem few days ago, then finally i got solution, i just run command update-package from package manager console and It works for me.

answered on Stack Overflow Jul 20, 2017 by Rajat-Systematix
0

If you are using VS2017 or later, update all the problem-creating references from NuGet. Now create a test project (using MVC template) and update all the solution references from NuGet. Now, take those portions (only those whose references are creating problems) of the web.config (both of solution root and Views) from this newly created project and replace the like portions in your original project's web.config. I solved similarly in my case.

[N.B. There were some miscellaneous issues in the process which I resolved uninstalling and reinstalling some references from NuGet, and building individual projects independently.]

answered on Stack Overflow May 22, 2019 by ashubuntu

User contributions licensed under CC BY-SA 3.0