63
votes

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?

13
Unfortunately, I need to run MVC 3 to reproduce this issue but all the answers are useless because they only tell me to upgrade. - wha7ever

13 Answers

76
votes

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

13
votes

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

9
votes

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.

6
votes

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

  Install-Package -Id  Microsoft.AspNet.WebHelpers
5
votes

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>
3
votes

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.

1
votes

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.

1
votes

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"

1
votes

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

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

1
votes

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

After that I solved my project with Web API.

0
votes

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.

0
votes

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
0
votes

Install-Package -Id Microsoft.AspNet.WebHelpers

Install-Package Microsoft.AspNet.Mvc -Version 5.0.0

This worked for me