Breezejs Web api OData method not allowed

655 views Asked by At

I am either getting a 400, 404, or 405 error attempting to query my Web API OData Service.

My remote service name is configured to:

var remoteServiceName = 'http://localhost:50056/odata/';

In my entityManagerFactory I have Odata set:

breeze.config.initializeAdapterInstance('dataService', 'webApiOData', true);

And in my datacontext I am calling:

var manager = entityManagerFactory.newManager();

    return breeze.EntityQuery.from('Courses')
            .using(manager).execute()
            .then(success).catch(failed);

I am currently getting the error:

XMLHttpRequest cannot load http://localhost:50056/odata/$metadata. No 'Access-Control-Allow-Origin' header is present on the requested resource

I can access this path just fine in the browser. I've found several resources to suggest I need to set the httpProtocol in my web.config as follows:

<httpProtocol>
  <customHeaders>
    <add name="Access-Control-Allow-Origin" value="*" />
    <add name="Access-Control-Allow-Headers" value="*" />
  </customHeaders>
</httpProtocol>

But that just gives me a

XMLHttpRequest cannot load http://localhost:50056/odata/$metadata. The 'Access-Control-Allow-Origin' header contains multiple values

I have also tried to set these settings from IIS Express's applicationhost config file but that gives me the following:

Invalid HTTP status code 400

I have also heard that adding the following setting to WebApiConfig should work:

config.EnableCors();

But I see no effect, and alternatively I have tried:

var cors = new EnableCorsAttribute(origins: "*", headers: "*", methods: "*");
config.EnableCors(cors);

Which also has no effect. I don't see what else I could be missing as I've exhausted every resource I've found online.

I am using Visual Studio 2013 express and using IIS express.

2

There are 2 answers

1
girlcode On

I found my problem. I was using OData v4. Apparently datajs does not support OData v4 yet. Adding the following code in the WebApiConfig smoothed things out even further:

var cors = new EnableCorsAttribute("*", "*", "*", "DataServiceVersion, MaxDataServiceVersion");
config.EnableCors(cors);
1
Ward On

Ahh. At the moment the Web Api implementation of OData is full of pot holes ... including but not limited to lagging data.js support.

I'm in touch with the OData team and hopeful we can make progress soon.

Do you really need OData? Unless you need to support open clients, it is much better to go straight Web Api.