Witam.
Lecimy dalej z problemami. Ktoś na forum polecił mi OData do mojego przypadku i jest to bardzo fajne narzędzie. Problem zaczyna się gdy chce zrobić własny response model. Dane są bardzo podstawowe i w pewnych przypadkach to co jest domyślnie wystarczy, ale nie u mnie. Drugą sprawą jest obsługa wyjątków.
try
{
return Ok(await _dataReader.GetCustomers("database_name"));
}
catch (Exception ex)
{
return StatusCode(500, ex.Message);
}
Nie mogę sobie tego tak zrobić, ponieważ OData oczekuje, że zostanie zwrócona lista obiektów, którą parametrami pofitruje, a endpoint w tym miejscu zwraca wyjątek i OData już sobie z tym nie radzi:
{
"error": {
"code": "",
"message": "The query specified in the URI is not valid. The requested resource is not a collection. Query options $filter, $orderby, $count, $skip, and $top can be applied only on collections.",
"details": [],
"innererror": {
"message": "The requested resource is not a collection. Query options $filter, $orderby, $count, $skip, and $top can be applied only on collections.",
"type": "Microsoft.OData.ODataException",
"stacktrace": " at Microsoft.AspNetCore.OData.Query.EnableQueryAttribute.ValidateSelectExpandOnly(ODataQueryOptions queryOptions) in Microsoft.AspNetCore.OData.dll:token 0x60002ff+0x3c\r\n at Microsoft.AspNetCore.OData.Query.EnableQueryAttribute.ExecuteQuery(Object responseValue, IQueryable singleResultCollection, ControllerActionDescriptor actionDescriptor, HttpRequest request) in Microsoft.AspNetCore.OData.dll:token 0x60002fa+0x2f\r\n at Microsoft.AspNetCore.OData.Query.EnableQueryAttribute.OnActionExecuted(ActionExecutedContext actionExecutedContext, Object responseValue, IQueryable singleResultCollection, ControllerActionDescriptor actionDescriptor, HttpRequest request) in Microsoft.AspNetCore.OData.dll:token 0x60002f5+0x97"
}
}
}
Dwa pytania:
- Jak ogarnąć custom response używając OData?
- Jak ogarnąć wyjątki?