.net – 无法重写Access_Control_Allow_Origin

我有一个网站就像我的其他网站的cdn一样.

我在Web.config中添加了以下内容

<httpProtocol>
  <customHeaders>
    <add name="Access-Control-Allow-Headers" value="Origin, X-Requested-With, Content-Type, Accept" />
    <add name="Access-Control-Allow-Methods" value="POST,GET,OPTIONS,PUT,DELETE" />
    <add name="Arr-Disable-Session-Affinity" value="True" />
  </customHeaders>
</httpProtocol>

<rewrite>
  <outboundRules>
    <clear />
    <rule name="AddCrossDomainHeader">
      <match serverVariable="RESPONSE_Access_Control_Allow_Origin" pattern=".*" />
      <conditions logicalGrouping="MatchAll" trackAllCaptures="true">
        <add input="{HTTP_ORIGIN}" pattern="(http(s)?://((.+\.)?[a-zA-Z0-9-]*\.ap\.dk|(.+\.)?localhost\:[0-9]*))" />
      </conditions>
      <action type="Rewrite" value="{C:0}" />
    </rule>
  </outboundRules>
</rewrite>

我在Access-control-allow-origin with multiple domains的答案#2中受到启发

但Access_Control_Allow_Origin的重写只适用于localhost.在实时网站上,它不会被重写,然后我得到这样的错误:

Failed to load 07001: The ‘Access-Control-Allow-Origin’ header has a value ‘07002’ that is not equal to the supplied origin. Origin ‘07003’ is therefore not allowed access

为了加载这个’Footer.html’,我必须在我的浏览器中清除缓存,如果我打开另一个要求这个的网站,请重复此操作.

最佳答案 你能尝试这样吗?

安装包Microsoft.AspNet.WebApi.Cors

打开文件App_Start / WebApiConfig.cs.

public static void Register(HttpConfiguration config)
        {

            config.EnableCors(); //add this


        }
点赞