404 Error when calling for the metadata using WEB API OData v4

不羁的心 提交于 2019-12-22 10:44:00

问题


I have tested the the call to the $metadata using postman and have found that when the MaxDataServiceVersion header is supplied the service always returns a 404 error. I have other posts about this using Breeze, but the best answer I can find simply says to comment out the line that adds this header.

We are using SAPUI5 ODataModel in our client application and commenting out that header is not an option.

Web API Config:

 ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
        builder.EntitySet<TBLMATERIAL>("TBLMATERIALs").EntityType.HasKey(o => o.MaterialNumber);

        config.MapODataServiceRoute(
            routeName: "ODataRoute",
            routePrefix: null,
            model: builder.GetEdmModel());

Client Code:

var oModel = sap.ui.model.odata.v2.ODataModel("http://localhost:56720/");

    oModel.attachMetadataFailed(null, function(a,b,c){
        alert(a);
    }, null);

    oModel.attachMetadataLoaded(null, function(){
       debugger;
       var oMetadata = oModel.getServiceMetadata();
       console.log(oMetadata);
    },null);

回答1:


You cannot really use OData v4 with SAPUI5. It is misleading that SAPUI5 provides a v4 data model class (sap.ui.model.odata.v4.ODataModel), which even in late 2019 is not yet production ready. I.e. you cannot send any custom headers with the v4.ODataModel, which would be key to authentication mechanisms. Any software company I know of uses the v2 model (sap.ui.model.odata.v2) to communicate with SAP and/or middleware services.

The 404 error you get is the result of SAPUI5 sending requests with specific v2 headers to your v4 API. As others have stated in their comments, MaxDataServiceVersion is one of these headers. DataServiceVersion is another one. In OData v4 these headers have been replaced by headers with totally different names (OData-Version, OData-MaxVersion).

I have actually gone a long way to try to intercept and modify these headers and establish a communication between SAPUI5's v2 model and .NET's v4 model. But in the end I failed and wasted a lot of time.

Bottomline

Use OData v2 in your .Net Framework WebApi if you want to communicate with SAPUI5!

To do so, you have to use the OData Nuget Package Microsoft.AspNet.WebApi.OData which comes in version 5.x. This package supports OData v1 to v3.

You may not use the latest OData Nuget Package Microsoft.AspNet.OData with version number 7.x. This package only supports OData v4 and will not make you happy.

If you are on .Net Core, you have no alternative but to use OData v4. There is no Nuget package for OData v2 that supports .Net Core. Your only solution in this scenario is, to use v4 server side and read/write it with normal JSON model REST API requests in SAPUI5. You will use convenience and functionality with that approach, but it can be done.



来源:https://stackoverflow.com/questions/34206491/404-error-when-calling-for-the-metadata-using-web-api-odata-v4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!