带有 OData 服务 4.x 的 Microsoft OData 客户端 6.x 在客户端上处理 NULL return(404 未找到)

Microsoft OData Client 6.x with OData service 4.x handle NULL return (404 Not Found) on the client

当为给定实体调用 OData 函数时,我似乎无法弄清楚如何在客户端上处理 NULL(404 未找到)。

Ex> 调用类似 "Context.Objects.ByKey(1).SomeFunction().GetValue()"
的服务 我想从服务中获取 "NULL" 但在客户端上它抛出了 404 Not Found 异常。

如果我将服务更改为 return "NULL" 那么我将在服务器上收到一个序列化异常,如果我告诉服务器 return "OK(null)" 我会也得到一个序列化异常。

这是控制器的服务器代码

[HttpGet]
public IHttpActionResult SomeFunction([FromODataUri] int key)
{
    string something = null;

    // Do some check and adjust the variable "something"

    if (string.IsNullOrWhiteSpace(something))
    {
        return NotFound();
    }
    else
    {
        return Ok(something);
    }
}

这里是 WebApiConfig 代码

builder.EntityType<SomeObject>().Function("SomeFunction").Returns<string>();

在使用 Microsoft OData 客户端时,我似乎找不到 "proper" 处理来自 odata 服务的空值的方法。

也许我可以连接到客户端 "ReceivingResponse" 事件来处理 404 Not Found 怎么办?任何建议...

OData 客户端的默认行为是在 OData 服务 returns 未找到 404 文件时抛出异常。

为了解决这个问题,OData 客户端上有一个 属性 生成的代码,名为 "IgnoreResourceNotFoundException"。
将此 属性 设置为 true 并且不会引发异常。