updated validation example for web api

Posted by MonkeyBonkey on Stack Overflow See other posts from Stack Overflow or by MonkeyBonkey
Published on 2012-10-14T15:22:27Z Indexed on 2012/10/14 15:37 UTC
Read the original article Hit count: 159

Filed under:
|
|
|

I tried the validation example code for web api on the latest mvc 4 download and I'm getting some errors. Does anyone have an updated example of the ValidationActionFilter class.

Here's the original code

public class ValidationActionFilter : ActionFilterAttribute 
{ 
    public override void OnActionExecuting(HttpActionContext context) 
    { 
        var modelState = context.ModelState; 
        if (!modelState.IsValid) 
        { 
            dynamic errors = new JsonObject(); 
            foreach (var key in modelState.Keys) 
            { 
                var state = modelState[key]; 
                if (state.Errors.Any()) 
                { 
                    errors[key] = state.Errors.First().ErrorMessage; 
                } 
            } 

            context.Response = new HttpResponseMessage<JsonValue>(errors, HttpStatusCode.BadRequest); 
        } 
    } 
}

I'm getting an error on HttpResponseMessage

The non-generic type 'System.Net.Http.HttpResponseMessage' cannot be used with type arguments

Also it looks like I need to add a Json reference, but should I be using JSON.net instead? An example of this using Json.net?

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET