0

I can't seem to figure out how to handle NULL (404 Not Found) on the client when calling an OData function for a given Entity.

Ex> calling service like "Context.Objects.ByKey(1).SomeFunction().GetValue()"
I want to get "NULL" from the service but instead on the client it throws a 404 Not Found exception.

If I alter the service to return "NULL" then I will receive a serialization exception on the server and if I tell the server to return "OK(null)" I will also get a serialization exception.

Here is the server code for the controller

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

And here is the WebApiConfig code

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

I can't seem to find the "proper" way of handling null values from the odata service when using Microsoft OData client.

Maybe I can wire into the client "ReceivingResponse" event to handle the 404 Not Found some how? Any suggestions...

4

1 回答 1

1

OData 客户端的默认行为是在 OData 服务返回 404 File Not Found 时引发异常。

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

于 2017-09-12T11:43:27.323 回答