MethodAccessException inside System.Web.WebPages migrating mvc 3 to mvc 5

大憨熊 提交于 2019-12-12 11:13:22

问题


Using Visual Studio 2013, I migrated a hybrid Asp.Net Webforms/MVC 3/Framework 4.0 web application to Asp.Net Webforms/MVC 5.2.2/Framework 4.5.1.

I followed the steps outlined in these links:

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

http://www.asp.net/signalr/overview/signalr-20/getting-started-with-signalr-20/upgrading-signalr-1x-projects-to-20

When I run the application in visual studio I get this error:

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.

I have tried these suggestions that I found on StackOverflow, but none fix the problem:

  1. clear the bin folder and rebuild
  2. Install Nuget package Microsoft.Aspnet.WebHelpers.
  3. Make sure all web.config references of System.Web.Mvc and System.Web.WebPages point to the correct version (5.2.2.0 and 3.0.0.0 respectively).

Any advice on how to find the cause of this error would be much appreciated.

The full stack trace of the error:

[MethodAccessException: 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.]
   System.Web.WebPages.Administration.SiteAdmin.RegisterAdminModule() +96
   System.Web.WebPages.Administration.PreApplicationStartCode.Start() +41

[InvalidOperationException: The pre-application start initialization method Start on type System.Web.WebPages.Administration.PreApplicationStartCode threw an exception with the following error message: 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..]
   System.Web.Compilation.BuildManager.InvokePreStartInitMethodsCore(ICollection`1 methods, Func`1 setHostingEnvironmentCultures) +559
   System.Web.Compilation.BuildManager.InvokePreStartInitMethods(ICollection`1 methods) +132
   System.Web.Compilation.BuildManager.CallPreStartInitMethods(String preStartInitListPath, Boolean& isRefAssemblyLoaded) +102
   System.Web.Compilation.BuildManager.ExecutePreAppStart() +153
   System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters, PolicyLevel policyLevel, Exception appDomainCreationException) +516

[HttpException (0x80004005): The pre-application start initialization method Start on type System.Web.WebPages.Administration.PreApplicationStartCode threw an exception with the following error message: 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..]
   System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +9913572
   System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +101
   System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +254

回答1:


The problem was that the web application had a reference to a (Framework 1) version of System.Web.Administration.dll, I assume because this reference was needed a couple of years ago when the application moved from web forms to hybrid webforms/MVC 3.

I removed this reference and the problem disappeared. Apparently this dll is no longer used. I assume the runtime was fetching the conflicting version of System.Web.Administration from this dll instead of using the new version.




回答2:


I was getting the same error on Asp.Net Webforms/MVC 5.2.2/Framework 4.5.1. when I published to Azure (it was working fine on localhost).

The solution, obtained from http://craigwilson.id.au/2014/03/18/azurewap-system-methodaccessexception-attempt-by-security-transparent-method/ was:

  1. Install the package Microsoft.AspNet.WebHelpers. From the Package management console run the following command:

    Install-Package -Id Microsoft.AspNet.WebHelpers

  2. Check the deployment option "Remove additional files at destination" located at Publish... -> Settings -> File Publish Options.




回答3:


When I had this problem I also had to delete the contents of the obj folder.




回答4:


I've created a new web site from a template and was getting this error when web site was published to Azure. I've checked "remove extra files" and "precompile" which are unchecked by default and it helped. I didn't try any other options.



来源:https://stackoverflow.com/questions/25775234/methodaccessexception-inside-system-web-webpages-migrating-mvc-3-to-mvc-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!