我想使用基于ajax的组件(KendoUI)来读取/修改由WCF DataServices实现的OData端点上的实体。WCF DataService不支持预检OPTIONS请求?
服务实现是相当容易在首位:
public class MyFooService : DataService<FooContext>
{
public static void SetEntitySetAccessRules(IDataServiceConfiguration config)
{
config.SetEntitySetAccessRule("Foos", EntitySetRights.AllWrite);
}
}
现在,我期待能够修改使用PUT实体。 KendoUI提供了一个好的和简单的配置界面,并在生成PUT请求方面做得很好。
我们正在提出跨域请求并使用CORS。因此,例如,Firefox在发送PUT之前向OData服务发送预检OPTIONS请求。
不幸的是,服务端点似乎不支持OPTIONS开箱即用:对OPTIONS请求的响应是“501未实现”,并且为空的内容。至少我们管理的响应具有CORS标题如下:
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<!-- Enable cross-origin resource sharing -->
<!-- http://enable-cors.org/#how-asp.net -->
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Methods" value="POST, PUT, DELETE, GET, OPTIONS" />
<add name="Access-Control-Allow-Headers" value="content-Type, accept, origin, X-Requested-With" />
<add name="Access-Control-Allow-Credentials" value="true" />
</customHeaders>
</httpProtocol>
</system.webServer>
谷歌搜索这已经变成了一个挑战,因为“选项”是一个非常流行的名词...
我发现this article但看起来很,很复杂。我的意思是,OData是关于REST的,我无法想象WCF Data Services不提供简单的方法来允许预检请求,或者?
我认为WCF的OData服务在WCF上工作,并通过相同的WCF管道。如果是这样,请查看此链接[answer。](http://stackoverflow.com/questions/11597314/origin-http-localhost-is-not-allowed-by-access-control-allow-origin/11606701#11606701 )。恕我直言,它是在WCF中实现CORS的最简单方法。 – Obaid
我知道这不是你想要的,但我认为使用跨域请求的最简单方法就是使用IIS的反向代理。通过这种方式,您只能对自己的域进行操作,而IIS则将您的请求发送到其他域。然后,你的代码不需要知道其他域的任何信息。 – lontivero
随机偶然发现这一点;只是想我会链接到[this](https://stackoverflow.com/questions/19808069/wcf-dataservice-odata-and-cors)和[that](https://data.uservoice.com/forums/ 72027-wcf-data-services-feature-suggestions/suggestions/4450449-cors-support)以获取更多讨论。 – tne