183

Update: same for mvc 4 to mvc 5.

I started a new mvc 4 project and migrated an mvc 3 project in it (controllers/models/scripts etc). While everything compiles now i get the following error:

Attempt by security transparent method 'WebMatrix.WebData.PreApplicationStartCode.Start()' to access security critical method 'System.Web.WebPages.Razor.WebPageRazorHost.AddGlobalImport(System.String)' failed.

I can't find anything on the web with this error. The error hits before getting into the application start.

I tried reinstalling all assemblies with nuget, putting a 'clean' web.config to no avail.

Anyone had this problem before?

Elger Mensonides
  • 6,930
  • 6
  • 46
  • 69

16 Answers16

255

For me this error was because I did NOT have Microsoft.AspNet.WebHelpers installed after updating from MVC 4 to MVC 5. It was fixed by installing the NuGet package

Install-Package -Id  Microsoft.AspNet.WebHelpers
Michael
  • 3,222
  • 2
  • 21
  • 22
  • 1
    This solved the issue for me; it did create some namespace issues with my `Resources` class, but I changed it to `Properties.Resources.xxxxx` and the issues were fixed. – Cody Mar 19 '15 at 14:48
  • 2
    I ran this and it didn't work...but then fortunately I saw that in another answer, there's a second command to run: http://stackoverflow.com/a/22587521/482256 – Kevin Nelson Jun 10 '16 at 21:37
  • 1
    this fxed it for me but I also needed the below in assembly bindings `code` `code` – Marchief Jan 04 '17 at 16:47
  • I install above mentioned package and problem is gone. I used NUget UI to install it and that also installed all the dependencies for me – Atta H. May 17 '18 at 17:39
205

If you are getting the error

Attempt by security transparent method ‘WebMatrix.WebData.PreApplicationStartCode.Start()’ to access security critical method ‘System.Web.WebPages.Razor.WebPageRazorHost.AddGlobalImport(System.String)’ failed.

In order to fix this install this package using NuGet package manager.

Install-Package Microsoft.AspNet.WebHelpers

After that , probably you will get another error

Cannot load WebMatrix.Data version 3.0.0.0 assembly

to fix this install this package using NuGet package manager.

Install-Package Microsoft.AspNet.WebPages.Data
Kyle Trauberman
  • 25,414
  • 13
  • 85
  • 121
Anushka
  • 2,434
  • 1
  • 17
  • 14
  • I had this exact error when upgrading to VS 2015 RTM. Same code ran just fine with VS 2013, the only difference being a new .config file that 2015 generates for IIS that I suspect the 2013 IISExpress ignores. This answer fixed this case too, add both nugets. – angularsen Jul 22 '15 at 13:45
  • It should be mentioned that I was not successful in running my website with VS2015 RTM at all. Installing Microsoft.AspNet.WebHelpers fixed the YSOD, but then I got a timeout due to some sql access rights instead. The exact same code ran just fine in VS2013. – angularsen Jul 27 '15 at 12:35
  • 1
    I've encountered this problem using VS 2013 Express Edition when upgrading from ASP .NET MVC 4 to ASP .NET MVC 5. This was helpful since @Anushka told the second problem with a solution. – Jerameel Resco Sep 09 '15 at 08:01
  • I get this error when I tried to run my solution in VS 2015. It runs perfectly fine in VS 2013. Installing the 2 packages as suggested works but I am not sure why I need to install them. I have other solutions that I developed in VS 2013 that do not get this error in VS 2015. – John81 May 09 '16 at 14:49
  • Is there a way to fix this problem without adding these extra Nugets? We have a single developer on VS2015 and he is the only one getting the problem. Like John81, I have no idea why we should be forced to install the 2 nugets and would prefer not to. Again like John81, we have other MVC 5 projects that work fine for the 2015 developer, so there must be something particular to this one app that perhaps we can remove to get rid of the error - but what is it? – Simon Green May 11 '16 at 14:58
  • 2
    We've found that VS2015 decides to put some WebMatrix DLLs in the webapp's bin directory on compilation - VS2013 doesn't do that. I don't know where 2015 is getting these DLLs from, but they must be a different version to the DLLs that get loaded in from the GAC (I guess) when you start up the app that's been compiled by VS2013 without the WebMatrix DLLS explicitly included in the app bin directory. Note that, if you delete the 4 WebMatrix files from the 2015 output, and restart IIS, the problem goes away. – Simon Green May 11 '16 at 15:15
  • @SimonGreen I wound up tracing my "ghost" WebMatrix references to the MySql.Web library - that project references WebMatrix for the Membership bits (which I don't actually use). I would assume you have something similar. Not sure why VS2015 treats those references any different than 2013, though. – Tieson T. Dec 14 '16 at 20:30
19

For anyone landing here who is trying to upgrade from MVC 4 to MVC5, I was able to resolve this issue by following the instructions at 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.

I also had to install the "Microsoft.AspNet.WebApi.WebHost" package from nuget. But that's it.

Oh, and I had to create this appSetting: <add key="owin:AutomaticAppStartup" value="false" />

:)

Alex Dresko
  • 5,179
  • 3
  • 37
  • 57
15

I tried all of the above solutions and it still wouldn't work, until I found that the web.config compilation element was referencing version 2.0.0.0 of WebMatrix.Data and WebMatrix.WebData. Changing the version of those entries in the web.config to 3.0.0.0 helped me.

Paul
  • 3,748
  • 1
  • 25
  • 28
11

I've been struggling with a similar issue when upgrading mvc 4 to mvc 5:

Attempt by security transparent method 'System.Web.WebPages.Administration.SiteAdmin.RegisterAdminModule()' to access security critical method 'System.Web.WebPages.ApplicationPart..ctor(System.Reflection.Assembly, System.String)' failed.

The delete extra files check box in the publish dialog solved it for me.

Sachin
  • 2,152
  • 1
  • 21
  • 43
Edward Olamisan
  • 800
  • 1
  • 18
  • 28
  • I had this error in my SharePoint 2013 Provider Hosted App (ASP.NET MVC 5, deployed to Azure), "delete additional files" in the publish dialog solved it for me, too. – Anatoly Mironov Jul 25 '14 at 15:04
8

Reinstalling Microsoft.AspNet.WebHelpers works for me

Update-Package –reinstall Microsoft.AspNet.WebHelpers
Pablo Claus
  • 5,886
  • 3
  • 29
  • 38
6

You may also get :

Could not load file or assembly 'WebMatrix.Data, Version=3.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)

This has moved to this package

 Install-Package Microsoft.AspNet.WebPages.Data

You should probably do a clean build before attempting any of the answers to this question and after updating packages

Simon_Weaver
  • 140,023
  • 84
  • 646
  • 689
6

Just one more suggestion... This was caused for me by some old dll's from an MVC 3 project after upgrading to MVC 5 in the site bin folder on the deployment server. Even though these dll's were no longer used by the code base they appeared to be causing the problem. Cleaned it all out and re-deployed and it was fine.

chris
  • 1,731
  • 4
  • 26
  • 33
3

Deleting all files from bin and then rebuilding the solution worked for me.

theskhan
  • 41
  • 3
3

Here is how I fixed this issue:

Open the nuget package manager console and install the below nuget packages:

Install-Package WebMatrix.Data
Install-Package Microsoft.AspNet.WebHelpers
Update-Package

Clean the solution, rebuild and my asp.net web app starts working!

MayogaX
  • 469
  • 3
  • 18
Mostafa
  • 3,296
  • 2
  • 26
  • 43
1

For me this error was caused by DotNetOpenAuth not being compatible with MVC5 after upgrading from MVC4 to MVC5. Uninstalling Microsoft.Web.WebPages.OAuth fixed the problem.

Dean North
  • 3,741
  • 2
  • 29
  • 30
1

I have removed it from my references.Then run this in Package Manager Console

Install-Package WebMatrix.Data

Finally add WebMatrix.WebData assembly to references,and rebuild project.It works for me.I hope it solves your problem too.

Ali Gol Gol
  • 775
  • 7
  • 14
0

I installed webapi with it via the helppages nuget package. That package replaced most of the asp.net mvc 4 binaries with beta versions which didn't work well together with the rest of the project. Fix was to restore the original mvc 4 dll's and all was good.

Elger Mensonides
  • 6,930
  • 6
  • 46
  • 69
0

I had the same problem, I had to update MVC Future (Microsoft.AspNet.Mvc.Futures)

Install-Package Microsoft.AspNet.Mvc.Futures
PvtVandals
  • 123
  • 1
  • 3
0

Just to give a really concrete example for a legacy solution I faced the problem right today (in 2023). In my case

  • using Visual Studio 2017
  • I upgraded AspNet.MVC from 4.0.0.1 to 5.2.9 running on .Net 4.5.2

But I still had the package

  • WebMatrix.WebData, Version 2.0.30506. for .Net4 installed

You have to

  • remove WebMatrix.WebData, Version 2.0 completely

That is what caused the error. As explained here and in other sources like [https://www.wikitechy.com/fix-error/attempt-by-security-transparent-method-web-matrix-web-data-pre-application-start-code-start] the installation of

  • Microsoft.AspNet.WebHelpers, Version 3.2.9

This will bring

  • WebMatrix.WebData, Version 3.0.0

Be sure that all other referenced assemblies are also updated that way.

Oliver S.
  • 109
  • 6
-1

For me this errors resolved by adding

<system.web>
 <trust level="Full">
 </system.web>

in web.config

Omar Kamel
  • 155
  • 1
  • 8