Attempt by 'System.Web.Mvc.PreApplicationStartCode.Start()' to critical method 'System.Web.WebPages.Razor.PreApplicationStartCode.Start()' failed

asp.net Mvc-4RazorDependencies

asp.net Mvc-4 Problem Overview


My application was working fine and after I did some upgrades I am getting this error.

>### Server Error in '/' Application.


> Attempt by security transparent method 'System.Web.Mvc.PreApplicationStartCode.Start()' to access security critical method 'System.Web.WebPages.Razor.PreApplicationStartCode.Start()' failed. 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.MethodAccessException: Attempt by security transparent method 'System.Web.Mvc.PreApplicationStartCode.Start()' to access security critical method 'System.Web.WebPages.Razor.PreApplicationStartCode.Start()' failed.

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

>Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18055

I cannot remember the updates I did. Is there a way to fix this?

asp.net Mvc-4 Solutions


Solution 1 - asp.net Mvc-4

I think the issue has been solved, but if not, use this package from Nuget:

Install-Package Microsoft.AspNet.Mvc -Version 5.0.0

I have wasted a day for this error but this single line has done the work for me

Solution 2 - asp.net Mvc-4

tried . . . Install-Package Microsoft.AspNet.Mvc -Version 5.0.0

and. . . Install-Package -Id Microsoft.AspNet.WebHelpers

both with no success, finally had to update all packages via nuget. . worked!!

http://www.3dbuzz.com/forum/threads/202082-ASP-net-MVC-Error-before-application-start

> If you're using visual studio open the nuget packages windows and run > Update-Package, this will update all your dll to the last version. If > it still not work, see this page: > 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

Solution 3 - asp.net Mvc-4

I got this error when deploying a site where Mvc had been upgraded from 4 to 5 but I had not updated web.config.

Changing from this:

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

to this:

<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
</dependentAssembly>
solved the problem for me.

Solution 4 - asp.net Mvc-4

For me also same error occured. It was fixed by installing the NuGet package

  Install-Package -Id  Microsoft.AspNet.WebHelpers

Solution 5 - asp.net Mvc-4

I had a similar problem, and solved it by based on the article Updating Razor 2.0 to 3.0 with Asp.net MVC by by Anthony Fassett

The following block of code was missing from my Web.Config:

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

Solution 6 - asp.net Mvc-4

In my case it was an incompatibility issue. I had theses pacakges:

  • Microsoft.AspNet.Mvc version 4.x
  • Microsoft.AspNet.Razor and Microsoft.AspNet.WebPages, version 3.x

I suppose that upgrading Mvc to 5.x would have worked, but I needed to keep Mvc 4.x, so i had to downgrade Razor and Mvc to 2.x. To do so, you just have to install the older version, like this:

Install-package Microsoft.AspNet.WebPages -version 2.0.30506
Install-Package Microsoft.AspNet.Razor -Version 2.0.30506

After downgrading these packages, the first error disappeared, but there was still another error related to a problem in web.config. I had to change the binding redirect for Razor:

<dependentAssembly>
     <assemblyIdentity name="System.Web.Razor" ... />
  <!--bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" /-->
     <bindingRedirect oldVersion="0.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
</dependentAssembly>

I don't know why, but the binding redirect wasn't updated by the Nuget installation, which should have done it.

Solution 7 - asp.net Mvc-4

There are multiple solutions for this problem:

  • Follow the tutorial on upgrading your asp.net solution
  • Don't forget to remove the Microsoft-Web-Helpers dll and install the nuget package Microsoft.AspNet.WebHelpers

Then, make sure you have upgraded all your nuget Packages (use "Upgrade-Package") and check your dll's in web.config.

The error is probably an action in an old DLL, when you are converting your project.

Solution 8 - asp.net Mvc-4

In my case, a nuget installation had changed my Web.Config:

    <dependentAssembly>
    <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="1.0.0.0-5.1.0.0" newVersion="1.1.0.0" />
  </dependentAssembly>

newVersion="1.1.0.0"** should be newVersion="5.1.0.0"

Solution 9 - asp.net Mvc-4

Right Click to Project -> Manage Nuget Packages for solution than find

Microsoft.AspNet.Mvc and Microsoft.Net.Compilers update latest versions

Solution 10 - asp.net Mvc-4

I solved my problem by applying the "Install-Package Microsoft.AspNet.Mvc" command.

After that I solved my project with Web API.

Solution 11 - asp.net Mvc-4

I received this error when I upgraded a NuGet package - 'Microsoft.AspNet.Web Pages' version 3.2.3. I am using VS2012, starting a new vanilla 'ASP.NET MVC 4 Web Application' with template 'Internet Application' using Windows Authentication or Windows Azure authentication.

Perhaps the update is not compatible with other components, and these other components must also be upgraded. Other answers suggest updating Microsoft.AspNet.Mvc, Microsoft.Net.Compilers, and/or Microsoft.AspNet.WebHelpers.

Solution 12 - asp.net Mvc-4

I am working on ASP.NET Web API project and fetch same problem after adding following nuget CacheCow.Server.EntityTagStore.SqlServer

It solved installing this one

Install-Package Microsoft.AspNet.Mvc

Solution 13 - asp.net Mvc-4

Install-Package -Id Microsoft.AspNet.WebHelpers

Install-Package Microsoft.AspNet.Mvc -Version 5.0.0

This worked for me

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTylarBenView Question on Stackoverflow
Solution 1 - asp.net Mvc-4VivekhView Answer on Stackoverflow
Solution 2 - asp.net Mvc-4defcdeView Answer on Stackoverflow
Solution 3 - asp.net Mvc-4Fredrik StolpeView Answer on Stackoverflow
Solution 4 - asp.net Mvc-4dnyaneshwarView Answer on Stackoverflow
Solution 5 - asp.net Mvc-4user3383724View Answer on Stackoverflow
Solution 6 - asp.net Mvc-4JotaBeView Answer on Stackoverflow
Solution 7 - asp.net Mvc-4NicoJuicyView Answer on Stackoverflow
Solution 8 - asp.net Mvc-4Michael RodriguesView Answer on Stackoverflow
Solution 9 - asp.net Mvc-4HadnazzarView Answer on Stackoverflow
Solution 10 - asp.net Mvc-4Alison AlvesView Answer on Stackoverflow
Solution 11 - asp.net Mvc-4barrypickerView Answer on Stackoverflow
Solution 12 - asp.net Mvc-4reza.cse08View Answer on Stackoverflow
Solution 13 - asp.net Mvc-4priyankaView Answer on Stackoverflow