我正在使用ServiceStack構建REST Web服務。我想允許跨域請求,所以我註冊了CorsFeature插件。ServiceStack在選項請求上返回405
我APPHOST如下所示:
public class HomeAppHost : AppHostHttpListenerBase
{
public Context Context { get; set; }
public HomeAppHost(Context context)
: base("HomeAutomation", typeof(HomeInterfaceService).Assembly)
{
Context = context;
}
public override void Configure(Funq.Container container)
{
Plugins.Add(new CorsFeature());
Routes
.Add<HomeInterface>("/HomeInterface")
.Add<HomeInterface>("/HomeInterface/{Id}")
.Add<ViewModel>("/ViewModel")
.Add<FunctionInput>("/Function")
;
}
}
然後,當一個OPTIONS請求的服務進行,這導致405不允許的方法:
請求:
OPTIONS /Function HTTP/1.1
Host: localhost:1337
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:18.0) Gecko/20100101 Firefox/18.0 FirePHP/0.7.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: nl,en-us;q=0.7,en;q=0.3
Accept-Encoding: gzip, deflate
DNT: 1
Origin: http://localhost
Access-Control-Request-Method: POST
Access-Control-Request-Headers: content-type
x-insight: activate
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
迴應:
HTTP/1.1 405 Method Not Allowed
Content-Length: 1837
Content-Type: application/xml
Server: Microsoft-HTTPAPI/2.0
Date: Fri, 15 Feb 2013 20:19:33 GMT
編輯
添加一個空的選項方法對服務確實防止405被觸發。然而,反應似乎是空的:
HTTP/1.1 200 OK
Transfer-Encoding: chunked
Server: Microsoft-HTTPAPI/2.0
Date: Sat, 16 Feb 2013 08:44:21 GMT
添加以下也給我一個空的迴應:
RequestFilters.Add((httpReq, httpRes, requestDto) =>
{
//Handles Request and closes Responses after emitting global HTTP Headers
if (httpReq.HttpMethod == "OPTIONS")
httpRes.End();
});
我不得不改變httpReq.Method到httpReq.HttpMethod和httpRes.EndServiceStackRequest()到httpRes.End()。它是否正確?
任何想法VB.net版本如何? – Iladarsda 2013-07-19 12:34:56
嘗試http://converter.telerik.com/ – sroes 2013-07-21 17:28:12