Error: Inheritance security rules violated by type: 'System.Web.WebPages.Razor.WebPageRazorHost'

泄露秘密 提交于 2019-11-27 14:22:11

问题


Out of nowhere my ASP.NET MVC 4 solution gives me this error:

Inheritance security rules violated by type: 'System.Web.WebPages.Razor.WebPageRazorHost'. Derived types must either match the security accessibility of the base type or be less accessible.

I Googled the problem and everybody says it has happened when anyone updates the solution from one version to another. But I didn't update my solution, just change the IDE from VS12 to VS13. Is this what creates the problem?

The full stack trace is as follows:

[TypeLoadException: Inheritance security rules violated by type: System.Web.WebPages.Razor.WebPageRazorHost'. Derived types must either match the security accessibility of the base type or be less accessible.] System.Web.WebPages.Razor.RazorBuildProvider.get_CodeCompilerType() +0 System.Web.Compilation.BuildProvider.GetCompilerTypeFromBuildProvider(BuildProvider buildProvider) +59 System.Web.Compilation.BuildProvidersCompiler.ProcessBuildProviders() +209 System.Web.Compilation.BuildProvidersCompiler.PerformBuild() +30 System.Web.Compilation.BuildManager.CompileWebFile(VirtualPath virtualPath) +9971917 System.Web.Compilation.BuildManager.GetVPathBuildResultInternal(VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile, Boolean throwIfNotFound, Boolean ensureIsUpToDate) +299 System.Web.Compilation.BuildManager.GetVPathBuildResultWithNoAssert(HttpContext context, VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile, Boolean throwIfNotFound, Boolean ensureIsUpToDate) +103 System.Web.Compilation.BuildManager.GetVirtualPathObjectFactory(VirtualPath virtualPath, HttpContext context, Boolean allowCrossApp, Boolean throwIfNotFound) +165 System.Web.Compilation.BuildManager.GetObjectFactory(String virtualPath, Boolean throwIfNotFound) +33 System.Web.Mvc.BuildManagerWrapper.System.Web.Mvc.IBuildManager.FileExists(String virtualPath) +40 System.Web.Mvc.BuildManagerViewEngine.FileExists(ControllerContext controllerContext, String virtualPath) +54 Microsoft.Web.Mvc.FixedRazorViewEngine.Microsoft.Web.Mvc.IViewEngineProxy.FileExists(ControllerContext controllerContext, String virtualPath) +42 Microsoft.Web.Mvc.ViewEngineFixWorker1.FileExists(ControllerContext controllerContext, String virtualPath) +57 Microsoft.Web.Mvc.<>c__DisplayClass4.<GetPathFromGeneralName>b__0(String path) +66 System.Web.WebPages.DefaultDisplayMode.GetDisplayInfo(HttpContextBase httpContext, String virtualPath, Func2 virtualPathExists) +90 System.Web.WebPages.<>c__DisplayClassb.b__8(IDisplayMode mode) +66 System.Linq.WhereSelectListIterator2.MoveNext() +103 System.Linq.Enumerable.FirstOrDefault(IEnumerable1 source, Func2 predicate) +94 System.Web.WebPages.DisplayModeProvider.GetDisplayInfoForVirtualPath(String virtualPath, HttpContextBase httpContext, Func2 virtualPathExists, IDisplayMode currentDisplayMode, Boolean requireConsistentDisplayMode) +297 System.Web.WebPages.DisplayModeProvider.GetDisplayInfoForVirtualPath(String virtualPath, HttpContextBase httpContext, Func2 virtualPathExists, IDisplayMode currentDisplayMode) +108 Microsoft.Web.Mvc.ViewEngineFixWorker1.GetPathFromGeneralName(ControllerContext controllerContext, List1 locations, String name, String controllerName, String areaName, String cacheKey, String[]& searchedLocations) +653 Microsoft.Web.Mvc.ViewEngineFixWorker1.GetPath(ControllerContext controllerContext, String[] locations, String[] areaLocations, String locationsPropertyName, String name, String controllerName, String cacheKeyPrefix, Boolean useCache, String[]& searchedLocations) +1508 Microsoft.Web.Mvc.ViewEngineFixWorker1.FindView(ControllerContext controllerContext, String viewName, String masterName, Boolean useCache) +329 Microsoft.Web.Mvc.FixedRazorViewEngine.FindView(ControllerContext controllerContext, String viewName, String masterName, Boolean useCache) +66 System.Web.Mvc.<>c__DisplayClassc.<FindView>b__b(IViewEngine e) +68 System.Web.Mvc.ViewEngineCollection.Find(Func2 lookup, Boolean trackSearchedPaths) +182 System.Web.Mvc.ViewEngineCollection.Find(Func2 cacheLocator, Func2 locator) +110 System.Web.Mvc.ViewEngineCollection.FindView(ControllerContext controllerContext, String viewName, String masterName) +329 System.Web.Mvc.ViewResult.FindView(ControllerContext context) +135 System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +230 System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +39 System.Web.Mvc.<>c__DisplayClass1a.b__17() +74 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func1 continuation) +388 System.Web.Mvc.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19() +72 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList1 filters, ActionResult actionResult) +303 System.Web.Mvc.Async.<>c__DisplayClass2a.b__20() +155 System.Web.Mvc.Async.<>c__DisplayClass25.b__22(IAsyncResult asyncResult) +184 System.Web.Mvc.Async.WrappedAsyncResult1.End() +136 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +40 System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__18(IAsyncResult asyncResult) +40 System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +47 System.Web.Mvc.Async.WrappedAsyncResult1.End() +151 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40 System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +44 System.Web.Mvc.Async.<>c__DisplayClass4.b__3(IAsyncResult ar) +47 System.Web.Mvc.Async.WrappedAsyncResult1.End() +151 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40 System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +39 System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +39 System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__3(IAsyncResult asyncResult) +45 System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +47 System.Web.Mvc.Async.WrappedAsyncResult1.End() +151 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40 System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +40 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +38 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9651116 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155


回答1:


From my friend Claudio Redi's comment the solusion of this problem is:

Try removing the System.Web.Razor assembly binding in the web.config if present. http://forums.asp.net/t/1968686.aspx?Installed+Nuget+package+Razor+Engine+whole+system+stops




回答2:


Go to Web.config and Search for the Assembly name="System.Web.Razor" and comment as follow.

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

After Commenting save and run the project,it will work fine.




回答3:


For me commenting of "System.Web.Razor" from web config did not worked.Here are the things i did to resolve this. Search your whole solution for references to MvcWebRazorHostFactory. you will find something like below in config file.

<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />

Change Version=4.0.0.0 to Version=5.0.0.0 and it will work.




回答4:


Commenting out the binding redirect as suggested above worked for 3 of my older machines but did not work on a fourth, newer machine.

Turns out that commenting out the binding redirect apparently just allows System.Web.Razor version 1.0.0.0 to be found/used, but my 4th machine, a newly built machine, lacked System.Web.Razor.DLL version 1.0.0.0.

Copying System.Web.Razor version 1.0.0.0 DLL to that machine in the website's bin folder, then my problem was solved. Removing 1.0.0.0 version DLL and putting version 2.0.0.0 back, the problem returned.

Another discovery I made regarding the machines where the bindingRedirect approach did work was that once RazorEngine worked for me after having commented out the binding redirect, I could un-comment-out the binding redirect and RazorEngine continued to work.




回答5:


Your project references are in turn linked to different versions of Microsoft.AspNet.Razor library, that are incompatibles between them.

Your stack trace points to System.Web.WebPages dll, which is probably linking to a version of the Microsoft.AspNet.Razor older that the one referenced by other NuGet package (for example Microsoft.AspNet.Mvc).

To fix the problem:

  1. If you are directly referencing the System.Web.WebPages, remove the reference and start using the System.Web.WebPages NuGet package
  2. If you are already using the System.Web.WebPages NuGet package, update it to the same version of the Microsoft.AspNet.Razor package that you are already using in your project.

This will fix your current error, but probably will result in a new one appearing, especially if you are migrating or mixing packages between Microsoft.AspNet.Mvc versions. In this case, you need to sync the versions of all your NuGet packages related to MVC.




回答6:


I was facing this error "inheritance security rules violated by type" in a Windows Service that used WCF components.

The same service worked on other server, but in a specific one, it didn't.

What fixed this error was commenting the following line in the app.config file.

<NetFx40_LegacySecurityPolicy enabled="true" />

We took a lot to figure it out, hope it helps somebody.



来源:https://stackoverflow.com/questions/27576750/error-inheritance-security-rules-violated-by-type-system-web-webpages-razor-w

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