[asp.net-mvc-3] 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:

  <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?

This question is related to asp.net-mvc-3 razor dependencies asp.net-4.0

The answer is


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.


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.

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


So simple. In Nuget Package Manager Console:

Update-Package Microsoft.AspNet.Mvc -Reinstall


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.


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


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

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 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.


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.


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 downgraded via NuGet to MVC 4 and then upgraded again to 5.2.7 and it fixed this issue


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.


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


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.


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.


Examples related to asp.net-mvc-3

Better solution without exluding fields from Binding IIs Error: Application Codebehind=“Global.asax.cs” Inherits=“nadeem.MvcApplication” Can we pass model as a parameter in RedirectToAction? return error message with actionResult Why is this error, 'Sequence contains no elements', happening? Failed to load resource: the server responded with a status of 500 (Internal Server Error) in Bind function 500.19 - Internal Server Error - The requested page cannot be accessed because the related configuration data for the page is invalid String MinLength and MaxLength validation don't work (asp.net mvc) How to set the value of a hidden field from a controller in mvc How to set a CheckBox by default Checked in ASP.Net MVC

Examples related to razor

Uncaught SyntaxError: Invalid or unexpected token How to pass a value to razor variable from javascript variable? error CS0103: The name ' ' does not exist in the current context how to set radio button checked in edit mode in MVC razor view @Html.DropDownListFor how to set default value Razor MVC Populating Javascript array with Model Array How to add "required" attribute to mvc razor viewmodel text input editor How to correctly use Html.ActionLink with ASP.NET MVC 4 Areas Multiple radio button groups in MVC 4 Razor How to hide a div element depending on Model value? MVC

Examples related to dependencies

Upgrading React version and it's dependencies by reading package.json How do I deal with installing peer dependencies in Angular CLI? RHEL 6 - how to install 'GLIBC_2.14' or 'GLIBC_2.15'? Automatically create requirements.txt node.js TypeError: path must be absolute or specify root to res.sendFile [failed to parse JSON] MavenError: Failed to execute goal on project: Could not resolve dependencies In Maven Multimodule project npm install private github repositories by dependency in package.json Find unused npm packages in package.json Failed to load c++ bson extension Maven dependency update on commandline

Examples related to asp.net-4.0

The type is defined in an assembly that is not referenced, how to find the cause? ASP.net Getting the error "Access to the path is denied." while trying to upload files to my Windows Server 2008 R2 Web server Check if Cookie Exists How to fix 'Microsoft Excel cannot open or save any more documents' Could not load file or assembly 'System.Web.WebPages.Razor, Version=2.0.0.0 How to fix: Handler "PageHandlerFactory-Integrated" has a bad module "ManagedPipelineHandler" in its module list ValidateRequest="false" doesn't work in Asp.Net 4