当HTTP状态代码为204 No Content时,为什么ServiceStack不添加CORS头?

时间:2015-02-19 14:16:40

标签: servicestack cors

为什么 ServiceStack 在HTTP状态代码为 204 无内容时不添加 CORS 标头?

1 个答案:

答案 0 :(得分:2)

刚刚使用最新版本的ServiceStack进行了测试:

配置ServiceStack以针对nullvoid回复 204 NoContent

SetConfig(new HostConfig {
    Return204NoContentForEmptyResponse = true,
});

添加自定义CORS配置:

Plugins.Add(new CorsFeature(
    allowCredentials: true,
    allowedHeaders: "Content-Type, Allow, Authorization"));

添加返回voidnull回复的新服务:

[Route("/void-response")]
public class TestVoidResponse { }

[Route("/null-response")]
public class TestNullResponse { }

public class TestServices : Service
{
    public void Any(TestVoidResponse response) {}

    public object Any(TestNullResponse response)
    {
        return null;
    }
}

致电上述服务:

两个服务都使用CORS标头返回 204 NoContent

HTTP/1.1 204 No Content
Cache-Control: private
Content-Length: 0
Vary: Accept
Server: Microsoft-IIS/8.5
X-Powered-By: ServiceStack/4.00 Win32NT/.NET
Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS
Access-Control-Allow-Headers: Content-Type, Allow, Authorization
Access-Control-Allow-Credentials: true
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Thu, 19 Feb 2015 15:10:50 GMT