ChatGPT解决这个技术问题 Extra ChatGPT

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

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?

This may also cause the problem MSB3277: Found conflicts between different versions of the same dependent assembly that could not be resolved.

M
Mohamed Emad

So simple. In Nuget Package Manager Console:

Update-Package Microsoft.AspNet.Mvc -Reinstall


This happened to me too. I have 2 branches of the same project in git. One is webforms with .NET MVC 5 added and one is straight web forms. When I switch branches in Visual Studio 2015 from the webforms only to the version with .NET MVC, I get the same error. This answer worked for me but I had to restart Visual Studio first.
i have this issue in the webapi 2 because i added mvc controller by mistake then issues begin after i removed the references ,so just uninstall-Package Microsoft.AspNet.Mvc solve my problem
For me, this method reinstalled Oauth and a bunch of other bloat, but did not fix the problem.
Thanks! I'd spent about a day on this till I saw your answer.
C
Camilo Martinez

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.

https://i.stack.imgur.com/SRpQM.png

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


Clicking Remove Unused References is as like using your hand to get a razor blade out of your kitchen sink disposal... Really Dangerous.
@cjbarth I would definitely backup my project first.
Never, ever touch "Remove unused references". It is evil and never, ever works as advertised. It is coding suicide.
it happened to me after using "Clean Solution" in visual studio. Now I have three web projects inside that solution that don't work with three different errors. Worst advice I ever followed on stack overflow (on a different question).
"Never, ever touch "Remove unused references."" - @AndyC. I did touch it ONE time. And now I'm here. Thanks for the right answer!
D
Darin Dimitrov

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.


As a matter of fact, it relies on System.Web.WebPages.Razor v2.0.0.0 which is only at the Release Candidate stage, and if I install it, it is breaking my application. So the solution for me now will be to revert to the previous version of Microsoft.Web.Helpers.
I still can't get this to work. I've searched the entire solution for ANY instance of 2.0.0.0 and found NOTHING! There's not one single reference to ANY assembly with a 2 in it anywhere in my solution let alone System.Web.WebPages.Razor. I've also checked the actual version number of all the assemblies from the System.Web.WebPages namespace and they're all 3.0. 1. Uninstalled the MVC package from Nuget and reinstalled it. 2. Removed the assemblyBinding elements from Web.config. 3. Cleaned/rebuilt the solution. 4. Unloaded the solution and looked through the .proj file for any references.
Ami, did you check the web.config? I was quite frustrated recently and then found it in the web.config.
T
Tomas Kubes

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.


This solved the issue perfectly and easily, thanks. After, MVC 4 runs side by side with older versions.
T
Tomas Kubes

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.


t
twasbrillig

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.


m
mpwhitt

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.


Z
Zia Qammar

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


D
David Frodin

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.


I was upgrading from 4 to 5 as well and encountered various errors. This site helped me a lot.
After a few days I wiped the slate clean and tried this all again. I had 3 projects in my solution referencing MVC and I upgraded them all to 5.2.3, which caused this error. What I did not realize was one of those 3 was common to many other solutions and required 4.0.40804. I had to downgrade that common project's MVC but the rest were able to work on 5.2.3.
M
Mik

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.


D
Dirk Verheijke

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.

See also: Could not load file or assembly System.Web.WebPages.Razor, , Version=3.0.0.0 or one of its dependencies


K
KhaNguyen

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


S
SOS

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.


I
Iman

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.


O
OutstandingBill

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.


y
yasodha bitra

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.


R
Rob

I downgraded via NuGet to MVC 4 and then upgraded again to 5.2.7 and it fixed this issue


s
shayda s

Fix Your Problem

Remove WebMatrix.Data and WebMatrix.WebData in the References of your Project


This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review