【问题标题】:Webapi CORS not working with breezejs/Odata V4Webapi CORS 不能与微风js/Odata V4 一起使用
【发布时间】:2015-01-28 19:05:39
【问题描述】:

我正在尝试在我的 webapi 上启用 CORS,以便我可以从另一个项目中使用它使用轻风js。 在我的 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/$metadata 时,我得到了正确的 xml,其中包含上面配置的 EDM 模型数据。

从我的 webproject 中使用带有 Firefox 的breathjs,但是当它尝试查询 localhost:22594/$metadata 时,我收到消息未启用 cors,并且我不允许从该来源读取它。

我正在执行这样的微风查询:

// 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 配置。我已经尝试了很多东西,但我似乎无法弄清楚我做错了什么。

【问题讨论】:

    标签: c# asp.net-web-api cors odata breeze


    【解决方案1】:

    我可能已经找到了解决方案。首先,我不得不切换回 odata v3。事实证明,breezejs/datajas 还不兼容 v4。

    之后我遇到了一条新的错误消息,说它无法读取 null 的“结尾”。也找不到有关该消息的任何信息,因此进行了一些实验。 原来我所有的模型类都在各自的文件夹中。我为每个实体使用了不同的命名空间。

    来自微风的文档:(http://www.getbreezenow.com/documentation/odata-server)

    如果你的模型类有多个命名空间,你就会陷入困境。据我们所知,Web API OData 无法处理位于多个命名空间中的模型类。

    我将每个实体都放在同一个文件夹中,以便它们都使用相同的命名空间,瞧!我现在终于可以使用 odata 使用微风js 查询我的 webapi!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-04
      • 1970-01-01
      • 2013-09-01
      • 1970-01-01
      • 2015-11-15
      • 2016-02-27
      相关资源
      最近更新 更多