C# 让CORS和Nancy一起工作

C# 让CORS和Nancy一起工作,c#,asp.net,api,cors,nancy,C#,Asp.net,Api,Cors,Nancy,我正试图获得与南希和科尔斯合作的所有类型的请求。目前,我在请求末尾添加了一个管道: pipelines.AfterRequest.AddItemToEndOfPipeline((ctx) => ctx.Response .WithHeader("Access-Control-Allow-Origin", "http://localhost:57515") .WithHeader("Access-Control-Allo

我正试图获得与南希和科尔斯合作的所有类型的请求。目前,我在请求末尾添加了一个管道:

            pipelines.AfterRequest.AddItemToEndOfPipeline((ctx) => ctx.Response
            .WithHeader("Access-Control-Allow-Origin", "http://localhost:57515")
            .WithHeader("Access-Control-Allow-Methods", "POST, GET, DELETE, PUT, OPTIONS")
            .WithHeader("Access-Control-Allow-Headers", "Accept, Origin, Content-type")
            .WithHeader("Allow", "POST, GET, DELETE, PUT, OPTIONS"))
options请求返回时的状态代码为200,这使我相信它执行得很好,但对于options以外的任何类型的请求,它都会失败,不允许使用405方法。为了让它正常工作,我还需要在客户端或服务器端做些什么

我使用的客户端库是主干


提前感谢。

我发现在IIS重写器规则中处理CORS头更好这是一个示例重写webconfig部分,它可以:

<rewrite>
  <outboundRules>
    <rule name="Set Access Control Allow Origin Header" preCondition="Origin header">
      <match serverVariable="RESPONSE_Access-Control-Allow-Origin" pattern="(.*)" />
      <action type="Rewrite" value="{HTTP_ORIGIN}" />
    </rule>
    <rule name="Set Access Control Allow Headers Header" preCondition="Origin header">
      <match serverVariable="RESPONSE_Access-Control-Allow-Headers" pattern="(.*)" />
      <action type="Rewrite" value="Content-Type" />
    </rule>
    <rule name="Set Access Control Allow Methods Header" preCondition="Origin header">
      <match serverVariable="RESPONSE_Access-Control-Allow-Method" pattern="(.*)" />
      <action type="Rewrite" value="POST,GET,OPTIONS" />
    </rule>
    <rule name="Set Access Control Allow Credentials Header" preCondition="Origin header">
      <match serverVariable="RESPONSE_Access-Control-Allow-Credentials" pattern="(.*)" />
      <action type="Rewrite" value="true" />
    </rule>
    <preConditions>
      <preCondition name="Origin header" logicalGrouping="MatchAny">
        <add input="{HTTP_ORIGIN}" pattern="(http://localhost$)" />
        <add input="{HTTP_ORIGIN}" pattern="(http://.*\.YOURDOMAIN\.com$)" />
      </preCondition>
    </preConditions>
  </outboundRules>
</rewrite>


我认为您不需要将选项指定为允许的CORS方法。我从来没有看过那套,我也从来没有自己设置过。浏览器不会抱怨

另一方面,我的设置与您的类似:

public abstract class MyModule : NancyModule
    protected MyModule(bool hasRazorView = true)
        After.AddItemToEndOfPipeline((ctx) => ctx.Response
            .WithHeader("Access-Control-Allow-Origin", "*")
            .WithHeader("Access-Control-Allow-Methods", "POST,GET")
            .WithHeader("Access-Control-Allow-Headers", "Accept, Origin, Content-type"));
        . . . .
    }
}

在我的情况下,COR是通过我的get和POST发送的,但不是选项。我使用
OPTIONS[“/”]=route=>newresponse()
覆盖了默认选项处理。在.NET Core上,我能够使用以下配置方法中的代码通过CORS:

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public virtual void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        loggerFactory.AddConsole(Configuration.GetSection("Logging"));
        loggerFactory.AddDebug();

        // Shows UseCors with CorsPolicyBuilder.
        app.UseCors(builder =>
           builder.WithOrigins(new[] { "http://localhost:4200" }).AllowAnyHeader()
                    .AllowAnyMethod()
                    .AllowCredentials()
           );

        app.UseOwin(a => a.UseNancy(b => b.Bootstrapper = new NancyBootstrap(this.Container)));

    }

我不认为这是真正的答案,因为它取决于托管平台,如果您使用NancyFX和OWIN,您可以在各种平台上托管。这个问题似乎也没有提到IIS(这应该是答案。这对我很有帮助,非常感谢你。