对于带有 API 的 IIS MVC 5 / Angular CLI(是的,我很清楚您的问题出在 Angular JS)项目中,我执行了以下操作:
<system.webServer>
节点下的web.config
<staticContent>
<remove fileExtension=".woff2" />
<mimeMap fileExtension=".woff2" mimeType="font/woff2" />
</staticContent>
<httpProtocol>
<customHeaders>
<clear />
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Content-Type, atv2" />
<add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS"/>
</customHeaders>
</httpProtocol>
全局.asax.cs
protected void Application_BeginRequest() {
if (Request.Headers.AllKeys.Contains("Origin", StringComparer.OrdinalIgnoreCase) && Request.HttpMethod == "OPTIONS") {
Response.Flush();
Response.End();
}
}
这应该可以解决 MVC 和 WebAPI 的问题,而无需执行所有其他操作。然后我在 Angular CLI 项目中创建了一个 HttpInterceptor,它自动添加了相关的标头信息。希望这可以帮助遇到类似情况的人。