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

时间:2013-08-03 11:27:31

标签: c# asp.net-web-api odata cors 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);

这是我的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();
}

这是控制器:

[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);
    }
}

以下是我使用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);
    }

这是我得到的错误:

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

我不知道的是为什么查询的网址是:http://localhost:23758/odata/$metadata而不是http://localhost:23758/odata/$metadata#MuscleGroup

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

var cors = new EnableCorsAttribute("http://localhost:7122/", "*", "*"); 

长话短说,我的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

我不知道这个属性 end 是什么,因为它没有在我的实体中定义

1 个答案:

答案 0 :(得分:5)

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

尝试将DataServiceVersionMaxDataServiceVersion添加到EnableCorsAttribute。 这对我有用。我找到了here