我正在使用ODataController来获取我的查询结果。控制器如此明确:
public class RunController : ODataController
{
[EnableQuery(MaxNodeCount = 1000)]
public IHttpActionResult Get() {
...
}
}
如果我直接进入控制器就行了。设置了我的路线,以便我转到此网址:
http://localhost:58704/odata/Run
路线配置如下所示:
config.MapODataServiceRoute(
routeName: "defaultOdata",
routePrefix: "odata",
model: GetModel(),
batchHandler: new DefaultODataBatchHandler(GlobalConfiguration.DefaultServer));
GetModel()的位置如下:
public static Microsoft.OData.Edm.IEdmModel GetModel()
{
ODataModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<RunController.RunModel>("Run");
return builder.GetEdmModel();
}
如您所见,我已启用defaultOdataBatchHandler。
http://localhost:58704/odata/$batch
顺利工作。下一步是创建批处理语句,该语句由datajs完成,如下所示:
return OData.request({
requestUri: "http://localhost:58704/odata/$batch",
method: "POST",
data: {
__batchRequests: [
{requestUri: "Run", method: "GET" }
]
}
}, function (data, response) {
console.log(data.__batchResponses);
}, undefined, OData.batchHandler);
批处理查询获得它所需的内容,并返回HTTP 200.这太棒了。其中转换为http://localhost:58704/odata/Run
的查询返回HTTP 404.我不能为我的生活理解原因。
显示console.log(data.__batchResponses)
的行返回1个具有message属性的对象; &#34; HTTP请求失败&#34;,并在主体说的响应中:
"{"message":"No HTTP resource was found that matches the request URI 'http://localhost:58704/odata/Run'.","messageDetail":"No type was found that matches the controller named 'odata'."}"
如果我使用错误消息中显示的网址,它可以毫无障碍地工作。可能是批处理请求中的method: "GET"
无法正常工作吗?
答案 0 :(得分:2)
基于您在ASP.NET Web API OData应用程序中使用的类和方法,我认为您正在使用OData V4。 但是您的客户端(dataJS)不支持OData V4。 您可以使用支持OData V4的apache odatajs以及以下代码:
window.odatajs.oData.request({
requestUri: "/odata/$batch",
method: "POST",
data: {
__batchRequests: [
{ requestUri: "Products", method: "GET" }
]
}
}, function (data, response) {
console.log(data.__batchResponses[0].data.value);
}, undefined, window.odatajs.oData.batch.batchHandler);
两个库的API都没有问题。
包含服务器端(ASP.NET Web API OData V4)和客户端(apache odatajs)的完整示例可以在此处下载
这些是两个请求之间的差异:
odatajs request headers(successful one):
OData-MaxVersion: 4.0
OData-Version: 4.0
Accept: application/json;q=0.9, */*;q=0.1
datajs request headers(failed one):
MaxDataServiceVersion: 3.0
DataServiceVersion: 1.0
Accept: application/atomsvc+xml;q=0.8, application/json;odata=fullmetadata;q=0.7, application/json;q=0.5, */*;q=0.1