我创建了一个使用Code First的WebApi 2.2 OData v1-3。这是我的ODataConfig
public static class ODataConfig
{
public static void Register(HttpConfiguration config)
{
#region Normal way to build EDM model the Microsoft way
ODataModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<Gear>("Gears");
...
#endregion
// enable CORS
var cors = new EnableCorsAttribute("*", "*", "*", "DataServiceVersion, MaxDataServiceVersion");
config.EnableCors(cors);
config.Routes.MapODataServiceRoute(
routeName: "odata",
routePrefix: "odata",
model: builder.GetEdmModel());
}
}
在我的Global.asax.cs中,我在Application_Start()
中调用它GlobalConfiguration.Configure(ODataConfig.Register);
我的控制器是ODataController
public class GearsController : ODataController
{...}
我可以构建项目并尝试那些没有问题的项目
localhost:51131/OData/Gears
{ &#34; odata.metadata&#34;:&#34; http://localhost:51131/odata/ $元数据#齿轮&#34;&#34;值&#34;:[ { &#34;标题&#34;:&#34; Burton 729&#34;,&#34;描述&#34;:&#34; Burton 720&#34;,&#34;尺寸&#34;:&#34 ; 12&#34;,&#34;缺陷&#34;:&#34;没有缺陷&#34;,&#34; PricePerDay&#34;:12.0,&#34; PricePerWeek&#34;:50.0,&#34 ; AuthorizedAmount&#34;:400.0&#34; LikeCount&#34;:1,&#34;观看次数&#34;:25,&#34; BootSize&#34;:10,&#34;横幅&#34 ;:空,&#34; OWNERID&#34;:3,&#34; ID&#34;:3 },{ &#34;标题&#34;:&#34; Burton 360&#34;,&#34;描述&#34;:&#34; Burton 360&#34;,&#34;尺寸&#34;:&#34 ; 10&#34;,&#34;缺陷&#34;:&#34;没有缺陷&#34;,&#34; PricePerDay&#34;:12.0,&#34; PricePerWeek&#34;:60.0,&#34 ; AuthorizedAmount&#34;:450.0&#34; LikeCount&#34;:1,&#34;观看次数&#34;:25,&#34; BootSize&#34;:11,&#34;横幅&#34 ;:空,&#34; OWNERID&#34;:1,&#34; ID&#34:4 } ] }
localhost:51131/OData/$metadata
然后我有一个客户,凉亭安装&#34; angular&#34;,&#34; breezejs&#34;,&#34; datajs&#34;。我创建了一个breezeODataService
(function () {
'use strict';
angular.module('tahoeApp.services.data').factory('breezeODataService', breezeODataService);
breezeODataService.inject = ['breeze', 'model'];
function breezeODataService(breeze, model) {
configureBreeze();
var serviceName = 'http://localhost:51131/odata';
var metadataStore = new breeze.MetadataStore();
var factory = {
newManager: newManager,
serviceName: serviceName,
metadataStore: metadataStore
};
return factory;
function configureBreeze() {
// use Web API OData to query and save
breeze.config.initializeAdapterInstance('dataService', 'webApiOData', true);
// convert between server-side PascalCase and client-side camelCase
breeze.NamingConvention.camelCase.setAsDefault();
}
function newManager() {
var mgr = new breeze.EntityManager(serviceName);
return mgr;
}
}})();
然后我尝试从GearsController获取所有Gear数据,如此
function getAll() {
var self = this;
debugger;
return EntityQuery.from('Gears')
.using(self.manager).execute()
.then(querySucceeded).catch(queryFailed);
function querySucceeded(data) {
debugger;
return data.results;
}
function queryFailed(error) {
throw error;
}
}
但它总是返回
queryFailed
带有消息:
XMLHttpRequest无法加载[http:] localhost:51131 / odata / $ metadata。无效的HTTP状态代码405 ionic.bundle.js:17822错误:元数据查询失败:[http] localhost:51131 / odata / $ metadata; 在createError(breeze.debug.js:16436) 在breeze.debug.js:16214 在XMLHttpRequest.r.defaultHttpClient.request.f.onreadystatechange(datajs-1.1.2.min.js:13)