C# “System.Web.Http.GlobalConfiguration”的类型初始值设定项引发异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19091195/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 14:04:10  来源:igfitidea点击:

The type initializer for 'System.Web.Http.GlobalConfiguration' threw an exception

c#asp.net-mvccors

提问by

I have added a new Web API project. I install Cors

我添加了一个新的 Web API 项目。我安装 Cors

PM> Install-Package Microsoft.AspNet.WebApi.Cors -Pre

Then when I run my project, I get this error:

然后当我运行我的项目时,我收到此错误:

The type initializer for 'System.Web.Http.GlobalConfiguration' threw an exception.

“System.Web.Http.GlobalConfiguration”的类型初始值设定项引发异常。

This is my inner exception:

这是我内心的例外:

{"Attempt by method 'System.Web.Http.GlobalConfiguration..cctor()' to access field 'System.Web.Http.GlobalConfiguration.CS$<>9__CachedAnonymousMethodDelegate2' failed."}

{“尝试通过方法'System.Web.Http.GlobalConfiguration..cctor()'访问字段'System.Web.Http.GlobalConfiguration.CS$<>9__CachedAnonymousMethodDelegate2'失败。”}

采纳答案by Adween

I came across the same issue and found a blog post on it. According to this blog post if you use the release candidate it should fix the error

我遇到了同样的问题,并找到了一篇关于它的博客文章。根据这篇博客文章,如果您使用候选版本,它应该修复错误

Install-Package Microsoft.AspNet.WebApi -IncludePrerelease

From here: http://wp.sjkp.dk/webapi-and-cors-enabled-rest-services/

从这里:http: //wp.sjkp.dk/webapi-and-cors-enabled-rest-services/

This worked for me :D

这对我有用:D

In other words, it was fixed in 5.1.0-rc1release.

换句话说,它是在5.1.0-rc1发布时修复的。

回答by Stephan

I had the same problem. Definitely "Cors" doesn't work to me!

我有同样的问题。绝对“Cors”对我不起作用!

What I did to solve my problem with Cross Domain in WebApi it was remove Cors and change my web.config

我为解决 WebApi 中的跨域问题所做的工作是删除 Cors 并更改我的 web.config

If you insert the following lines inside your web.config, you'll have an WebApi with Cross Domain enabled.

如果您在 web.config 中插入以下几行,您将拥有一个启用了跨域的 WebApi。

<system.webServer> <httpProtocol> <customHeaders> <add name="Access-Control-Allow-Origin" value="*" /> <add name="Access-Control-Allow-Methods" value="GET, POST, OPTIONS, PUT, DELETE" /> </customHeaders> </httpProtocol> </system.webServer>

<system.webServer> <httpProtocol> <customHeaders> <add name="Access-Control-Allow-Origin" value="*" /> <add name="Access-Control-Allow-Methods" value="GET, POST, OPTIONS, PUT, DELETE" /> </customHeaders> </httpProtocol> </system.webServer>

回答by FlimFlam Vir

I had this problem using NET 4.6.1 and after may hours of research removing this from web.config finally fixed the issue:

我在使用 NET 4.6.1 时遇到了这个问题,经过数小时的研究,从 web.config 中删除了这个问题,最终解决了这个问题:

<runtime>
<dependentAssembly>
     <assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
     <bindingRedirect oldVersion="0.0.0.0-4.1.1.0" newVersion="4.1.1.0" />
   </dependentAssembly>
</runtime>