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

18

67

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:

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

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?

Buckjump answered 12/6, 2012 at 16:8 Comment(1)
This may also cause the problem MSB3277: Found conflicts between different versions of the same dependent assembly that could not be resolved.Desorb
C
30

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.

Carce answered 12/6, 2012 at 16:12 Comment(3)
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.Anastigmatic
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.Palladic
Ami, did you check the web.config? I was quite frustrated recently and then found it in the web.config.Palter
R
69

So simple. In Nuget Package Manager Console:

Update-Package Microsoft.AspNet.Mvc -Reinstall

Ruelu answered 10/6, 2015 at 11:6 Comment(4)
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.Desorb
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 problemArgyle
For me, this method reinstalled Oauth and a bunch of other bloat, but did not fix the problem.Crookes
Thanks! I'd spent about a day on this till I saw your answer.Lithotomy
S
52

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.

Remove unused references

I fixed this problem by doing the following,

  1. Open 'Package Manager Console' in Visual Studio
  2. Uninstall-Package Microsoft.AspNet.Mvc
  3. Install-Package Microsoft.AspNet.Mvc
Schmeltzer answered 18/12, 2012 at 1:20 Comment(5)
Clicking Remove Unused References is as like using your hand to get a razor blade out of your kitchen sink disposal... Really Dangerous.Dearr
@cjbarth I would definitely backup my project first.Schmeltzer
Never, ever touch "Remove unused references". It is evil and never, ever works as advertised. It is coding suicide.Iq
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).Elli
"Never, ever touch "Remove unused references."" - @AndyC. I did touch it ONE time. And now I'm here. Thanks for the right answer!Hartshorn
C
30

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.

Carce answered 12/6, 2012 at 16:12 Comment(3)
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.Anastigmatic
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.Palladic
Ami, did you check the web.config? I was quite frustrated recently and then found it in the web.config.Palter
P
8

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.

Purgatory answered 15/10, 2013 at 21:9 Comment(1)
This solved the issue perfectly and easily, thanks. After, MVC 4 runs side by side with older versions.Bradfordbradlee
S
7

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.

Songster answered 16/3, 2013 at 16:31 Comment(0)
S
4

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

Sudanic answered 25/5, 2019 at 8:7 Comment(0)
H
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.

Hesper answered 7/3, 2017 at 22:30 Comment(0)
A
2

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.

Acatalectic answered 23/3, 2017 at 18:21 Comment(0)
W
1

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.

Walford answered 26/5, 2017 at 15:52 Comment(2)
I was upgrading from 4 to 5 as well and encountered various errors. This site helped me a lot.Rifling
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.Walford
M
1

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.

Monad answered 11/8, 2017 at 14:35 Comment(0)
G
1

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

Given answered 14/1, 2020 at 11:21 Comment(0)
A
1

Fix Your Problem

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

Arbe answered 10/2, 2022 at 9:53 Comment(2)
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 ReviewOrphaorphan
This! actually fixed my problem. I also downgraded Microsoft.aspnet.razor, Microsoft.AspNet.WebPages.Data, and Microsoft.AspNet.WebPages.WebData to version 3.0.0Enneagon
T
0

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

Tankoos answered 24/7, 2017 at 2:13 Comment(0)
R
0

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.

Rissa answered 25/10, 2017 at 12:41 Comment(0)
C
0

For similar assembly binding errors , following steps may help:

  1. Right click on your Solution and click Manage Nuget Packages for Solution ...
  2. 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)
  3. consolidate specially packages related to your assembly error and note that many packages are dependent on some other packages like *.code & *.api & ...
  4. after resolving all suspected consolidations , rebuild and rerun the app and see if the assembly bindings are resolved.
Conformity answered 16/3, 2019 at 8:28 Comment(0)
T
0

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.

Temikatemp answered 2/5, 2019 at 3:32 Comment(0)
I
0

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.

Interlinear answered 6/2, 2020 at 23:51 Comment(0)
T
0

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

Terrance answered 30/8, 2020 at 8:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.