Wie gehen Inhalt in der Antwort von Ausnahme-filter in Asp.net WebAPI?

Betrachten Sie folgenden code:

Mein problem ist:

1) ich kann nicht scheinen zu wirken, die Fehler zu HttpContent

2) kann ich nicht verwenden, die CreateContent Erweiterung Methode wie dieser nicht vorhanden ist auf den Kontext an.Antwort.Inhalt.CreateContent

Dem Beispiel hier scheint nur das zu bieten StringContent und ich möchte in der Lage sein zu und übergibt den Inhalt als JsobObject:
http://www.asp.net/web-api/overview/web-api-routing-and-actions/exception-handling

 public class ServiceLayerExceptionFilter : ExceptionFilterAttribute
    {
        public override void OnException(HttpActionExecutedContext context)
        {
            if (context.Response == null)
            {                
                var exception = context.Exception as ModelValidationException;

                if ( exception != null )
                {
                    var modelState = new ModelStateDictionary();
                    modelState.AddModelError(exception.Key, exception.Description);

                    var errors = modelState.SelectMany(x => x.Value.Errors).Select(x => x.ErrorMessage);

                    //Cannot cast errors to HttpContent??
                    //var resp = new HttpResponseMessage(HttpStatusCode.BadRequest) {Content = errors};
                    //throw new HttpResponseException(resp);

                    //Cannot create response from extension method??
                    //context.Response.Content.CreateContent
                }
                else
                {
                    context.Response = new HttpResponseMessage(context.Exception.ConvertToHttpStatus());
                }                
            }

            base.OnException(context);
        }

    }
InformationsquelleAutor jaffa | 2012-06-18
Schreibe einen Kommentar