使用WEB API OData v4调用元数据时出现404错误 [英] 404 Error when calling for the metadata using WEB API OData v4

查看:133
本文介绍了使用WEB API OData v4调用元数据时出现404错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用邮递员测试了对$ metadata的调用,发现当提供MaxDataServiceVersion标头时,该服务始终返回404错误.我还有其他关于使用Breeze的文章,但是我能找到的最佳答案只是说去注释添加此标头的行.

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.

我们在客户端应用程序中使用SAPUI5 ODataModel,并且注释掉该标头不是一个选择.

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

Web API配置:

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());

客户代码:

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

推荐答案

您不能真正将OData v4与SAPUI5一​​起使用. SAPUI5提供v4数据模型类(sap.ui.model.odata.v4.ODataModel)是令人误解的,即使在2019年末,该类仍未准备就绪. IE.您无法使用v4.ODataModel发送任何自定义标头,这对于身份验证机制至关重要.我认识的任何软件公司都使用v2模型(sap.ui.model.odata.v2)与SAP和/或中间件服务进行通信.

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.

您收到的404错误是SAPUI5将带有特定v2标头的请求发送到v4 API的结果.正如其他人在评论中指出的那样, MaxDataServiceVersion 是这些标头之一. DataServiceVersion 是另一个.在OData v4中,这些标头已由名称完全不同的标头( OData-Version OData-MaxVersion )替换.

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).

我实际上已经走了很长一段路要尝试截取和修改这些标头,并在SAPUI5的v2模型和.NET的v4模型之间建立通信.但是最后我失败了,浪费了很多时间.

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.

如果要与SAPUI5通信,请在.Net Framework WebApi中使用OData v2!

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

为此,您必须使用版本5.x中附带的OData Nuget软件包 Microsoft.AspNet.WebApi.OData .该软件包支持OData v1到v3.

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.

您可能使用版本号为7.x的最新OData Nuget软件包 Microsoft.AspNet.OData .该软件包仅支持OData v4,不会让您满意.

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.

如果您使用的是.Net Core,则只能使用OData v4.没有用于支持.Net Core的OData v2的Nuget程序包.在这种情况下,您唯一的解决方案是使用v4服务器端,并使用SAPUI5中的常规JSON模型REST API请求对其进行读写.您将通过这种方法使用便利性和功能性,但是可以做到这一点.

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.

这篇关于使用WEB API OData v4调用元数据时出现404错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆