CORS使用asp.net web api 2 odata和breeze

hyp*_*erN 2 c# odata cors asp.net-web-api breeze

我在使用Breeze消耗我的OData时遇到了问题.我的api托管在另一台服务器上,我使用的是asp.net web api 2.0(随VS 2013预览一起提供).我知道web api已正确配置为CORS,因为我已经测试了它没有微风,它工作正常.

这是支持CORS的web api 2.0代码:

var cors = new EnableCorsAttribute("http://localhost:7122/", "*", "*");
config.EnableCors(cors);
Run Code Online (Sandbox Code Playgroud)

这是我的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();
}
Run Code Online (Sandbox Code Playgroud)

这是控制器:

[EnableCors(origins: "http://localhost:7122", headers: "*", methods: "*")] //this enables CORS for controller 
public class MuscleGroupController : EntitySetController<MuscleGroup, int>
{
    private readonly DatabaseContext _databaseContext = new DatabaseContext();

    [Queryable]
    public override IQueryable<MuscleGroup> Get()
    {
        return _databaseContext.MuscleGroups;
    }

    protected override MuscleGroup GetEntityByKey(int key)
    {
        return _databaseContext.MuscleGroups.Find(key);
    }
}
Run Code Online (Sandbox Code Playgroud)

以下是我使用breeze消费OData的方法:

app.adminMuscleGroup.dataService = ( function(breeze, logger) {

    breeze.config.initializeAdapterInstances({ dataService: "OData" });

    var servicename = "http://localhost:23758/odata/";

    var manager = new breeze.EntityManager(servicename);

    manager.enableSaveQueuing(true);

    var dataService = {
        getAll: getAll,
    };

    return dataService;

    function getAll() {
        var query = breeze.EntityQuery.from("MuscleGroup").orderBy("Name");

        return manager.executeQuery(query);
    }
Run Code Online (Sandbox Code Playgroud)

这是我得到的错误:

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
Run Code Online (Sandbox Code Playgroud)

我没有得到的是为什么查询的URL是:http://localhost:23758/odata/$metadata而不是http://localhost:23758/odata/$metadata#MuscleGroup

我知道这个答案解释了如何使用带有CORS的breeze,但我相信这是先前的web api 2并且我不需要为CORS处理编写类,因为我现在可以使用:

var cors = new EnableCorsAttribute("http://localhost:7122/", "*", "*"); 
Run Code Online (Sandbox Code Playgroud)

长话短说,我的api很好地处理CORS(我已经测试过了)但是由于某些原因它不适用于微风.

编辑

[EnableCors(origins: "http://localhost:7122", headers: "*", methods: "*")]从控制器中删除了这一行,并且刚刚启用了CROS globbaly,但现在我收到此错误:

[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
Run Code Online (Sandbox Code Playgroud)

我不知道这是什么性质到底是的,因为它没有在我的实体定义

Pau*_*hal 5

var cors = new EnableCorsAttribute(
    "http://localhost:7122/",
    "*",
    "*",
    "DataServiceVersion, MaxDataServiceVersion"
);
config.EnableCors(cors);
Run Code Online (Sandbox Code Playgroud)

尝试添加DataServiceVersionMaxDataServiceVersion你的EnableCorsAttribute.这对我有用.我在这里找到.