Updated validation example for web api

I tried the verification example code for web api in the latest mvc 4 download and I am getting some errors. Does anyone have an updated example of the ValidationActionFilter class.

Here is the source 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 get an HttpResponseMessage error message

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

It also looks like I need to add a Json link, but should I use JSON.net instead? An example of this using Json.net?

+4
source share
1 answer

HttpResponseMessage<T> from beta version, it no longer exists in version version, use below:

  actionContext.Response = actionContext.Request .CreateResponse(HttpStatusCode.BadRequest, errors); 
+2
source

Source: https://habr.com/ru/post/1439652/


All Articles