asp.net-mvc-4 – WebAPI OData 5.0 Beta – 访问GlobalConfiguration会抛出安全错误

前端之家收集整理的这篇文章主要介绍了asp.net-mvc-4 – WebAPI OData 5.0 Beta – 访问GlobalConfiguration会抛出安全错误前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我最近安装了WebApi OData 5.0框架的预发布版本,以便使用新的$expand支持.一切都建好了,但是我在App_Start上得到了一个奇怪的例外.
Attempt by security transparent method
'System.Web.Http.GlobalConfiguration.get_Configuration()' 
to access security critical type 'System.Web.Http.HttpConfiguration' Failed.

错误

Line 12:    protected void Application_Start()
Line 13:    {
Line 14:        WebApiConfig.Register(GlobalConfiguration.Configuration); // <--
Line 15:    }

堆栈跟踪:

[TypeAccessException: Attempt by security transparent method 'System.Web.Http.GlobalConfiguration.get_Configuration()' to access 
security critical type 'System.Web.Http.HttpConfiguration' Failed.]
   System.Web.Http.GlobalConfiguration.get_Configuration() +0
   API.WebApiApplication.Application_Start() in Global.asax.cs:14

[HttpException (0x80004005): Attempt by security transparent method 'System.Web.Http.GlobalConfiguration.get_Configuration()' to access 
security critical type 'System.Web.Http.HttpConfiguration' Failed.]
   System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext context,HttpApplication app) +12863325
   System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext,HttpContext context,MethodInfo[] handlers) +175
   System.Web.HttpApplication.InitSpecial(HttpApplicationState state,MethodInfo[] handlers,IntPtr appContext,HttpContext context) +304
   System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext,HttpContext context) +404
   System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext) +475

[HttpException (0x80004005): Attempt by security transparent method 'System.Web.Http.GlobalConfiguration.get_Configuration()' to access 
security critical type 'System.Web.Http.HttpConfiguration' Failed.]
   System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +12880068
   System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +159
   System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr,HttpContext context) +12721257

Google显示的很少.

解决方法

在包管理器控制台中按照给定的顺序运行以下命令:
Uninstall-Package Microsoft.AspNet.Mvc.FixedDisplayModes
Update-Package Microsoft.AspNet.Mvc -Pre
Update-Package Microsoft.AspNet.WebApi -Pre
Update-Package Microsoft.AspNet.WebApi.Tracing

现在,对web.config应用以下更改:

>在项目的Web.config中,将应用设置网页值更新为3.0.0.0
>在web.config的运行时段下,检查已组态的每个程序集的版本,并将其更新为添加到项目中的程序集的版本.以下是我的web.config中的更新配置:

<运行时间> < AssemblyBinding xmlns =“urn:schemas-microsoft-com:asm.v1”> < dependentAssembly> < assemblyIdentity name =“System.Web.Helpers”publicKeyToken =“31bf3856ad364e35”/> < bindingRedirect oldVersion =“1.0.0.0-3.0.0.0”newVersion =“3.0.0.0”/> < / dependentAssembly> < dependentAssembly> < assemblyIdentity name =“System.Web.Mvc”publicKeyToken =“31bf3856ad364e35”/> < bindingRedirect oldVersion =“1.0.0.0-5.0.0.0”newVersion =“5.0.0.0”/> < / dependentAssembly> < dependentAssembly> < assemblyIdentity name =“System.Web.WebPages”publicKeyToken =“31bf3856ad364e35”/> < bindingRedirect oldVersion =“1.0.0.0-3.0.0.0”newVersion =“3.0.0.0”/> < / dependentAssembly> < dependentAssembly> < assemblyIdentity name =“EntityFramework”publicKeyToken =“b77a5c561934e089”/> < bindingRedirect oldVersion =“0.0.0.0-5.0.0.0”newVersion =“5.0.0.0”/> < / dependentAssembly> < dependentAssembly> < assemblyIdentity name =“WebGrease”publicKeyToken =“31bf3856ad364e35”/> < bindingRedirect oldVersion =“0.0.0.0-1.3.0.0”newVersion =“1.3.0.0”/> < / dependentAssembly> < dependentAssembly> < assemblyIdentity name =“System.Web.Http”publicKeyToken =“31bf3856ad364e35”culture =“neutral”/> < bindingRedirect oldVersion =“0.0.0.0-5.0.0.0”newVersion =“5.0.0.0”/> < / dependentAssembly> < dependentAssembly> < assemblyIdentity name =“System.Net.Http.Formatting”publicKeyToken =“31bf3856ad364e35”culture =“neutral”/> < bindingRedirect oldVersion =“0.0.0.0-5.0.0.0”newVersion =“5.0.0.0”/> < / dependentAssembly> < / assemblyBinding> < /运行>

从Views文件夹打开web.config.这里有三件事要更新:

>在configSections下,将Razor程序集的版本更新为3.0.0.0.
>在system.web.webPages.razor部分更新主机的版本,将System.Web.Mvc.MvcWebRazorHostFactory的版本更新为3.0.0.0.
> System.web的页面部分提到了一些版本号.将它们更新到5.0.0.0版本

如果您安装了Web API帮助页面,请检查以上装配体的配置.

猜你在找的asp.Net相关文章