OData (v3 en azından beri) hataları temsil etmek specific json kullanır:
{
"error": {
"code": "A custom error code",
"message": {
"lang": "en-us",
"value": "A custom long message for the user."
},
"innererror": {
"trace": [...],
"context": {...}
}
}
}
Microsoft .Net bir sunucu tarafında OData hatayı oluşturmak üzere Microsoft.Data.OData.ODataError ve Microsoft.Data.OData.ODataInnerError sınıflarını içerir.
uygun OData hata yanıt (
HttpResponseMessage) oluşturulması için, bu hata olabilir ayrıntıları içerir:
System.Web.OData.Extensions.HttpRequestMessageExtensions.CreateErrorResponse yöntemi kullanılarak
1) form ve kontrolörün eylem HttpResponseMessage geri
return Request.CreateErrorResponse(HttpStatusCode.Conflict, new ODataError { ErrorCode="...", Message="...", MessageLanguage="..." }));
2) kullanılarak HttpResponseException atmak HttpResponseMessage
3) özel yazılan özel durum oluşturmak için aynı yöntem ve
public class CustomExceptionFilterAttribute : ExceptionFilterAttribute
{
public override void OnException(HttpActionExecutedContext context)
{
if (context.Exception is CustomException)
{
var e = (CustomException)context.Exception;
var response = context.Request.CreateErrorResponse(e.StatusCode, new ODataError
{
ErrorCode = e.StatusCodeString,
Message = e.Message,
MessageLanguage = e.MessageLanguage
});
context.Response = response;
}
else
base.OnException(context);
}
}
Anahtar artırmalıdır bulunamadı Web Api eylem filtreleri kullanarak dönüştürmek 404 – qujck