【发布时间】:2012-11-06 01:14:31
【问题描述】:
我正在使用https://github.com/ServiceStack/ServiceStack/wiki/New-Api 中描述的方法来启用 CORS。这似乎可行,即我在响应中得到了正确的访问控制标头,但状态码是 404。这有关系吗?
为了完整起见,我更愿意返回一个非错误代码。阅读http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html 似乎我应该返回 200。最好的方法是什么?
服务方法定义为:-
[EnableCors]
public void Options(ApiKeyRequest apiKeyRequest)
{
}
HTTP 请求是:-
OPTIONS http://myreallycoolwebapi/apikeyrequests HTTP/1.1
Host: myreallycoolwebapi
Connection: keep-alive
Access-Control-Request-Method: POST
Origin: http://localhost:8000
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11
Access-Control-Request-Headers: origin, content-type, accept
Accept: */*
Referer: http://localhost:8000/index.html
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-GB,en-US;q=0.8,en;q=0.6
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
响应是:-
HTTP/1.1 404 Not Found
Server: nginx
Date: Sat, 17 Nov 2012 18:06:01 GMT
Content-Type: text/plain; charset=utf-8
Content-Length: 3
Connection: keep-alive
Cache-Control: private
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS
Access-Control-Allow-Headers: Content-Type
404
更新 1
这是我的资源类型。
[Route("/apikeyrequests", "POST")]
public class ApiKeyRequest : IReturn<ApiKeyRequest>
{
public string EmailAddress { get; set; }
}
POST 请求工作正常。
【问题讨论】:
-
您能否展示一下您正在使用的 [EnableCors] 服务以及您提出的导致 404 错误的请求?
-
@mythz 我已经添加了这些 - 如果您需要更多信息,请告诉我
-
该死的,我自己也遇到过这个问题。会期望 405(根据 HTTP 规范)而不是 404。
标签: c# http servicestack cors