Search code examples
asp.net-web-api2odataodata-v4

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


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


Solution

  • The default behavior of the OData client is to throw an exception when the OData service returns a 404 File Not Found.

    To get around this there is a property on the OData Client generated code called "IgnoreResourceNotFoundException".
    Set this property to true and it with not throw an exception.