C# 使用asp.net web api 2 odata和breeze的CORS
我用Breeze喝我的OData有问题。我的api托管在另一台服务器上,我使用的是asp.net web api 2.0(VS 2013预览版附带)。我知道web api是为CORS正确配置的,因为我在没有breeze的情况下对其进行了测试,并且运行良好 以下是启用CORS的web api 2.0代码:C# 使用asp.net web api 2 odata和breeze的CORS,c#,asp.net-web-api,odata,cors,breeze,C#,Asp.net Web Api,Odata,Cors,Breeze,我用Breeze喝我的OData有问题。我的api托管在另一台服务器上,我使用的是asp.net web api 2.0(VS 2013预览版附带)。我知道web api是为CORS正确配置的,因为我在没有breeze的情况下对其进行了测试,并且运行良好 以下是启用CORS的web api 2.0代码: var cors = new EnableCorsAttribute("http://localhost:7122/", "*", "*"); config.EnableCors(cors);
var cors = new EnableCorsAttribute("http://localhost:7122/", "*", "*");
config.EnableCors(cors);
这是我的IEdmModel
private static IEdmModel CreateModel()
{
var modelBuilder = new ODataConventionModelBuilder {Namespace = "Test.Domain.Model"};
modelBuilder.EntitySet<MuscleGroup>("MuscleGroup");
modelBuilder.EntitySet<Exercise>("Exercises");
modelBuilder.EntitySet<ExerciseCategory>("ExerciseCategories");
modelBuilder.EntitySet<Muscle>("Muscle");
return modelBuilder.GetEdmModel();
}
下面是我得到的错误:
Failed to load resource: the server responded with a status of 400 (Bad Request) http://localhost:23758/odata/$metadata
Failed to load resource: Origin http://localhost:7122 is not allowed by Access-Control-Allow-Origin. http://localhost:23758/odata/$metadata
XMLHttpRequest cannot load http://localhost:23758/odata/$metadata. Origin http://localhost:7122 is not allowed by Access-Control-Allow-Origin. MuscleGroup:1
[Q] Unhandled rejection reasons (should be empty):
Array[0]
q.js:891
Error: Metadata query failed for: http://localhost:23758/odata/$metadata; Logger.js:52
我不明白为什么查询的url是:http://localhost:23758/odata/$metadata
而不是http://localhost:23758/odata/$metadata#肌肉群
我知道答案解释了如何使用breeze与CORS,但我相信这是以前的WebAPI 2,我不需要编写CORS处理类,因为我现在可以用以下方法来完成:
var cors = new EnableCorsAttribute("http://localhost:7122/", "*", "*");
长话短说,我的api很好地处理CORS(我已经测试过了),但由于某些原因,它不能与breeze一起工作
编辑
我已经删除了这一行[启用CORS(源代码:]http://localhost:7122,标题:“*”,方法:“*”)
来自控制器,并且刚刚启用了CROS Globaly,但现在我收到了以下错误:
[Q] Unhandled rejection reasons (should be empty):
Array[0]
q.js:891
Error: Metadata query failed for http://localhost:23758/odata/$metadata; Unable to process returned metadata: Cannot read property 'end' of null Logger.js:52
我不知道这个属性端是什么,因为它没有在我的实体中定义
var cors = new EnableCorsAttribute(
"http://localhost:7122/",
"*",
"*",
"DataServiceVersion, MaxDataServiceVersion"
);
config.EnableCors(cors);
尝试将dataservicevision
和maxdataservicevision
添加到您的EnableCorsAttribute
。
这对我有用。我找到了。我猜您遇到了以下问题:…您可以尝试通过删除结尾
/
来修改EnableCorSatAttribute设置,并查看它是否正确works@KiranChalla的问题不在于CORS(我删除了“/”,但没有帮助),我相信是smth。关于OData和Breeze,这里是我的另一个问题:看看我问的关于CORS OData和Breeze的问题
[Q] Unhandled rejection reasons (should be empty):
Array[0]
q.js:891
Error: Metadata query failed for http://localhost:23758/odata/$metadata; Unable to process returned metadata: Cannot read property 'end' of null Logger.js:52
var cors = new EnableCorsAttribute(
"http://localhost:7122/",
"*",
"*",
"DataServiceVersion, MaxDataServiceVersion"
);
config.EnableCors(cors);