Could not load file or assembly 'System.Web.WebPages.Razor, Version=2.0.0.0
asp.net Mvc-3RazorDependenciesasp.net 4.0asp.net Mvc-3 Problem Overview
I just upgraded packages in an MVC 3 project (ASP.net 4), and now I am getting the following error message when accessing a page in that project:
> Server Error in '/MyApplication' Application.
> --------------------------------------------------------------------------------
>
>
> Could not load file or assembly 'System.Web.WebPages.Razor,
> Version=2.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) Description: An unhandled exception occurred during the
> execution of the current web request. Please review the stack trace
> for more information about the error and where it originated in the
> code.
>
> Exception Details: System.IO.FileLoadException: Could not load file or
> assembly 'System.Web.WebPages.Razor, Version=2.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)
>
> Source Error:
>
>
>
>
> An unhandled exception was generated during the execution of the
> current web request. Information regarding the origin and location of
> the exception can be identified using the exception stack trace below.
>
>
> Assembly Load Trace: The following information can be helpful to
> determine why the assembly 'System.Web.WebPages.Razor,
> Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'
> could not be loaded.
>
>
>
>
>
> === Pre-bind state information === LOG: User = IIS APPPOOL\ASP.NET v4.0 LOG: DisplayName = System.Web.WebPages.Razor, Version=2.0.0.0,
> Culture=neutral, PublicKeyToken=31bf3856ad364e35 (Fully-specified)
> LOG: Appbase = file:///C:/.../ LOG: Initial PrivatePath = C:...\bin
> Calling assembly : Microsoft.Web.Helpers, Version=2.0.0.0,
> Culture=neutral, PublicKeyToken=31bf3856ad364e35.
> === LOG: This bind starts in default load context. LOG: Using application configuration file: C:...\web.config LOG: Using host
> configuration file:
> C:\Windows\Microsoft.NET\Framework64\v4.0.30319\aspnet.config LOG:
> Using machine configuration file from
> C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config.
> LOG: Post-policy reference: System.Web.WebPages.Razor,
> Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 LOG:
> Attempting download of new URL
> file:///C:/Windows/Microsoft.NET/Framework64/v4.0.30319/Temporary
> ASP.NET Files/.../cb62a7b2/327a0353/System.Web.WebPages.Razor.DLL.
> LOG: Attempting download of new URL
> file:///C:/Windows/Microsoft.NET/Framework64/v4.0.30319/Temporary
> ASP.NET
> Files/.../cb62a7b2/327a0353/System.Web.WebPages.Razor/System.Web.WebPages.Razor.DLL.
> LOG: Attempting download of new URL
> file:///C:/LeGroupeHtmSVN/.../UpDir.Web/bin/System.Web.WebPages.Razor.DLL.
> WRN: Comparing the assembly name resulted in the mismatch: Major
> Version ERR: Failed to complete setup of assembly (hr = 0x80131040).
> Probing terminated.
>
> Stack Trace:
>
>
>
>
>
> [FileLoadException: Could not load file or assembly
> 'System.Web.WebPages.Razor, Version=2.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)]
> Microsoft.Web.Helpers.PreApplicationStartCode.Start() +0
>
> [InvalidOperationException: The pre-application start initialization
> method Start on type Microsoft.Web.Helpers.PreApplicationStartCode
> threw an exception with the following error message: Could not load
> file or assembly 'System.Web.WebPages.Razor, Version=2.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).]
> System.Web.Compilation.BuildManager.InvokePreStartInitMethods(ICollection`1
> methods) +11711966
> System.Web.Compilation.BuildManager.CallPreStartInitMethods() +465
> System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager
> appManager, IApplicationHost appHost, IConfigMapPathFactory
> configMapPathFactory, HostingEnvironmentParameters hostingParameters,
> PolicyLevel policyLevel, Exception appDomainCreationException) +1167
>
> [HttpException (0x80004005): The pre-application start initialization
> method Start on type Microsoft.Web.Helpers.PreApplicationStartCode
> threw an exception with the following error message: Could not load
> file or assembly 'System.Web.WebPages.Razor, Version=2.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).]
> System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +11700896
> System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context)
> +141 System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest
> wr, HttpContext context) +4869125
>
>
>
> -------------------------------------------------------------------------------- Version Information: Microsoft .NET Framework Version:4.0.30319;
> ASP.NET Version:4.0.30319.272
In my web.config file, and in my application, I am referencing System.Web.WebPages.Razor v1.0.0.0:
>
I don't even know whether I could be using System.Web.WebPages.Razor v2.0.0.0 with .Net 4.
Now, how do I find out what is referencing System.Web.WebPages.Razor v2.0.0.0?
asp.net Mvc-3 Solutions
Solution 1 - asp.net Mvc-3
So simple. In Nuget Package Manager Console:
Update-Package Microsoft.AspNet.Mvc -Reinstall
Solution 2 - asp.net Mvc-3
This problem started when I did 'Remove Unused References'. The website still worked on my local machine, but did not worked on the server after publishing.
I fixed this problem by doing the following,
- Open 'Package Manager Console' in Visual Studio
- Uninstall-Package Microsoft.AspNet.Mvc
- Install-Package Microsoft.AspNet.Mvc
Solution 3 - asp.net Mvc-3
> how do I find out what is referencing System.Web.WebPages.Razor v2.0.0.0?
Just read your exception stacktrace:
Calling assembly : Microsoft.Web.Helpers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
So it seems that you are using the Microsoft.Web.Helpers
which in turn depends on System.Web.WebPages.Razor, Version=2.0.0.0
.
Solution 4 - asp.net Mvc-3
After much pain, googling and hair pulling, I ended up uninstalling MVC 4 using nuget, deleting all references to MVC, razor and infrastructure from the web config, deleting the dlls from the bin folder - then using nuget to reinstall everything. It took less time then trying to figure out why the dlls did not match.
Solution 5 - asp.net Mvc-3
In ASP.NET MVC 4, the namespace is not System.Web.WebPages.Razor, but System.Web.Razor
That worked for me, change your web.config.
Solution 6 - asp.net Mvc-3
I uninstalled ASP.NET MVC 4 using the Windows Control Panel, then reinstalled it by running AspNetMVC4Setup.exe
(which I got from https://www.microsoft.com/en-us/download/details.aspx?id=30683), and that fixed the issue for me.
In other words, I didn't need to use Nuget or Visual Studio.
Solution 7 - asp.net Mvc-3
I was getting the same error after adding an unnecessary reference to System.Web.Mvc
. I removed all the references I could find, but nothing seemed to work. I finally deleted the project's bin
folder and the error went away after a rebuild.
Solution 8 - asp.net Mvc-3
I also received this error and tried everything I could find online and it wouldn't go away. In the end, I just downgraded MVC from 5.2.3 to 4.0.40804. I don't like this solution because eventually I'll need to use MVC 5, but it works for now. Hope this helps others.
Solution 9 - asp.net Mvc-3
Short:
In you have this problem with a pure Web API project (and thus don't need razor), try to add it anyway, rebuild, then remove it.
Long story:
I had this problem with a brand-new pure Web API project, except that the stacktrace pointed "System.Web.Mvc" as Calling assembly (see Darin's answer).
No reference to MVC, Razor or anything like that in my project though...
I decided to add the MVC packages (AspNet.Mvc, AspNet.WebPages and AspNet.Razor) to check if there was any subsequent problem.
The WebApi app then launched perfectly fine. Then I removed the exact same packages and everything was still OK.
Hope it helps someone.
Solution 10 - asp.net Mvc-3
I was getting the same error when i upgrade MVC4 to MVC5 version, Firstly i Upgraded the calling assembly which was depends on
> System.Web.WebPages.Razor, Version=2.0.0.0
after that updated the web.config files under the Views folder, updated following packages from
<configSections>
<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>
</configSections>
to
<configSections>
<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=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
<section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
</sectionGroup>
</configSections>
and also updated
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
to
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.7.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
these steps works for me
Solution 11 - asp.net Mvc-3
After my PC got completely reinstalled, I got the error described here. VS 2017 was reinstalled but "ASP.NET Web Pages 2" was not reinstalled. After reinstalling "ASP.NET Web Pages 2", the problem was solved.
Solution 12 - asp.net Mvc-3
In some cases, just install web page 2 to resolve this (resolved with my case - deploy to local web page using web api )
https://www.microsoft.com/en-us/download/details.aspx?id=34600
Solution 13 - asp.net Mvc-3
If earlier working project crashing suddenly with mentioned error you can try following solution.
- Delete the bin folder of respective web/service project.
- Build
This worked for me.
Solution 14 - asp.net Mvc-3
For similar assembly binding errors , following steps may help:
- Right click on your Solution and click Manage Nuget Packages for Solution ...
- go to Consolidate Tab (last tab) and check if any any differences between packages installed in different projects inside your solution. specially pay attention to your referenced projects which may have lower versions because they usually are less noticed)
- consolidate specially packages related to your assembly error and note that many packages are dependent on some other packages like *.code & *.api & ...
- after resolving all suspected consolidations , rebuild and rerun the app and see if the assembly bindings are resolved.
Solution 15 - asp.net Mvc-3
I first tried Mohammed Emad's answer - no joy. Then I tried
git clean -x -d -f
which brought me to a new "Roslyn" error which I was able to fix by manually editing my .csproj.
Interestingly, after I'd read down a bit further down the page on the Roslyn question, I found another suggestion with even more votes (Update-Package Microsoft.CodeDom.Providers.DotNetCompilerPlatform -r
). Thinking I'd rather implement that than manually edit .csproj, I backed out my .csproj changes, only to find my solution was still working.
So after all that, I'm wondering if Mohammed's answer (on this page) would have done the trick, had I simply done the git clean first.
Solution 16 - asp.net Mvc-3
by installing AspNetMVC4Setup.exe ( Here is the link :https://www.microsoft.com/en-us/download/details.aspx?id=30683) solves the issue.
by restart/reinstalling Microsoft.AspNet.Mvc Package doesn't help me.
Solution 17 - asp.net Mvc-3
I downgraded via NuGet to MVC 4 and then upgraded again to 5.2.7 and it fixed this issue
Solution 18 - asp.net Mvc-3
Fix Your Problem
Remove WebMatrix.Data and WebMatrix.WebData in the References of your Project