ServiceStack Cors [英] ServiceStack Cors
问题描述
我正在建立一个项目,并希望实现CORS。它安装在共享的IIS服务器(共享主机)上。
在我的apphost.cs中,根据我在网上找到的几篇文章启用和配置CORS。
Plugins.Add(new CorsFeature(
allowedMethods:GET,POST,PUT,DELETE,OPTIONS,
allowedOrigins:*,
allowCredentials:true,
allowedHeaders:content-type,Authorization,Accept));
我还读到当实现较新的SS API(IService)时,我不得不提供一个钩子'option'call so I added ...
this.RequestFilters.Add((httpReq,httpRes,requestDto)=&
{
//发出全局HTTP头后处理请求并关闭响应
if(httpReq.HttpMethod ==OPTIONS)
httpRes.EndRequest(); //添加'using ServiceStack;'
});
我可以通过REST控制台调用我的api(chrome addin),但是当我调用ajax请求我收到了405错误。
请求URL:http://empire.thecodingpit.com/api/engine
请求方法:选项
状态码:200 OK
请求标头视图源
接受:* / *
接受编码:gzip,deflate,sdch
Accept-Language:en-US,en; q = 0.8
访问控制请求头:accept,origin,content-type
访问控制请求方法:POST
连接:keep-alive
主机:empire.thecodingpit.com
原产地:http://json.commublogs.com
参考:http://json.commublogs.com/
User-Agent:Mozilla / 5.0(Windows NT 6.1; WOW64)AppleWebKit / 537.36(KHTML,像Gecko)Chrome / 29.0.1547.66 Safari / 537.36
响应头视图源
Cache-Control:private
Content-Length:0
日期:Thu,26 Sep 2013 17:00:59 GMT
服务器:Microsoft-IIS / 8.0
X-AspNet版本:4.0.30319
的X MiniProfiler-IDS:f8c3ddb8434149feb689dd44d93bf862,6d0e7e0f8ac1456d98872a82af4d6602,67bdf08a19c641a7b26db0b43fd10888,1819b1ce3e314c0594ef9ecb1ac68fcf,7af8595373e345f3a9f8ade60c8a7817]
的X技术,通过:ASP.NET
我的方法没有任何[身份验证]。
我查看了此问题,并如上所示实现了它。这个相关的问题不是重复的。 servicestack REST API和CORS
我正在做的服务调用(POST)是...
public object Post(SelectedTroops request)
{
return new SelectedTroopsResponse {ArrayOfSelectedTroops = request.ArrayOfSelectedTroops};
}
如果我遗漏任何可能有帮助的内容,知道。我猜一个旁注问题可能是 - 你怎么有效地跟踪和调试这样的东西?
解决方案您必须使用:
httpRes.EndRequest();
这很重要,它是一个扩展方法。
ServiceStack
命名空间。我建议使用像ReSharper这样的工具,使自动查找和引用扩展方法轻而易举。I am building a project and wish to implement CORS. It is installed on a shared IIS server (shared hosting).
In my apphost.cs I enable and configure CORS according to several of the articles I find on the web.
Plugins.Add(new CorsFeature( allowedMethods: "GET, POST, PUT, DELETE, OPTIONS", allowedOrigins: "*", allowCredentials: true, allowedHeaders: "content-type, Authorization, Accept"));
I also read that when implementing the newer SS API (IService) I had to provide a hook for the 'Options' call so I added...
this.RequestFilters.Add((httpReq, httpRes, requestDto) => { //Handles Request and closes Responses after emitting global HTTP Headers if (httpReq.HttpMethod == "OPTIONS") httpRes.EndRequest(); //add a 'using ServiceStack;' });
I am able to call my api via REST Console (chrome addin) but when I call with a ajax request I get a 405 error.
Request URL:http://empire.thecodingpit.com/api/engine Request Method:OPTIONS Status Code:200 OK Request Headers view source Accept:*/* Accept-Encoding:gzip,deflate,sdch Accept-Language:en-US,en;q=0.8 Access-Control-Request-Headers:accept, origin, content-type Access-Control-Request-Method:POST Connection:keep-alive Host:empire.thecodingpit.com Origin:http://json.commublogs.com Referer:http://json.commublogs.com/ User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36 Response Headers view source Cache-Control:private Content-Length:0 Date:Thu, 26 Sep 2013 17:00:59 GMT Server:Microsoft-IIS/8.0 X-AspNet-Version:4.0.30319 X-MiniProfiler-Ids: ["f8c3ddb8434149feb689dd44d93bf862","6d0e7e0f8ac1456d98872a82af4d6602","67bdf08a19c641a7b26db0b43fd10888","1819b1ce3e314c0594ef9ecb1ac68fcf","7af8595373e345f3a9f8ade60c8a7817"] X-Powered-By:ASP.NET
I do not have any [Authentication] on my methods - yet.
I have looked at this question and have implemented it as shown above. This related question is NOT a duplicate. servicestack REST API and CORS
The service call (POST) that I am making is...
public object Post(SelectedTroops request) { return new SelectedTroopsResponse { ArrayOfSelectedTroops = request.ArrayOfSelectedTroops }; }
If I am missing anything that may be helpful please let me know. I guess an side note question may be - how do you effectively trace thru and debug something like this? Any helpful hints would be more than appreciated.
解决方案You do have to use:
httpRes.EndRequest();
It's important, it's an extension method in the
ServiceStack
namespace. I recommend using Tools like ReSharper which makes auto-finding and referencing extension methods trivial.这篇关于ServiceStack Cors的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!