我正在尝试在我的webapi上启用CORS,这样我就可以使用微风to从另一个项目中使用它。在我的webapi中,我启用了OData V4,并创建了一个用于微风消费的edm模型,如下所示:
ODataModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<ModuleEntity>("ModuleEntities");
builder.EntitySet<DomainEntity>("DomainEntities");
builder.EntitySet<ApiUserEntity>("ApiUserEntities");
config.MapODataServiceRoute(
routeName: "ODataRoute",
routePrefix: "",
model: builder.GetEdmModel());这三个实体集都有自己的控制器,这些控制器来自于odatacontrollers。
我在我的webapi中启用了cors:
// see http://stackoverflow.com/questions/18032360/cors-using-asp-net-web-api-2-odata-and-breeze for last 2 parameters
var cors = new EnableCorsAttribute("*", "*", "*", "DataServiceVersion, MaxDataServiceVersion"); // origins, headers, methods
config.EnableCors(cors);当我尝试访问localhost:22594/$元数据时,我会得到正确的xml,其中包含上面配置的EDM模型数据。
然而,从我的when项目中,通过使用Firefox,我得到了这样的信息:当cors试图查询本地主机时,没有启用cors:22594/$元数据,并且不允许我从这个来源读取它。
我在执行微风的询问,就像这样:
// setup dataservice
this.dataService = new this.breeze.DataService({
serviceName: "http://localhost:22594/"
});
// initialize adapter with OData as dataservice
this.breeze.config.initializeAdapterInstances({ dataService: "OData" });
this.manager = new this.breeze.EntityManager({ dataService: this.dataService });
// query ApiUser entity
var query = this.breeze.EntityQuery().from("ApiUser");
// execute query
this.manager.executeQuery(query);我仍然是新手,所以现在,我不知道我的错误是在使用微风或我的webaopi配置。我尝试了很多事情,但我似乎找不出我做错了什么。
发布于 2014-11-30 22:42:20
我可能已经找到解决办法了。首先,我不得不切换回odata v3。事实证明微风is /datajas还不能与v4兼容。
之后,我遇到了一条新的错误消息,表示它不能读取null的“end”。也找不到关于这条信息的任何信息,所以做了一些实验。原来我所有的模型类都在它们各自的文件夹中。我为每个实体使用了不同的名称空间。
来自微风the文档:(http://www.getbreezenow.com/documentation/odata-server)
如果模型类有多个名称空间,就会陷入困境。据我们所知,Web无法处理位于多个命名空间.中的模型类。
我将每个实体放置在同一个文件夹中,以便它们都使用相同的名称空间和瞧!现在我终于可以使用odata查询我的webapi了!
https://stackoverflow.com/questions/27217163
复制相似问题