WebAPI OData validation error ModelState object not returned

I am creating an AngularJS web form to perform POST (insertion) into a table using the WebAPI setting as OData. I am trying to return a damaged validation ModelState object (in JSON format) to validate the corresponding fields in the form.

All I get is one line with all the details as a string (not in JSON analysis format)

{ "odata.error":{ "code":"","message":{ "lang":"en-US","value":"The request is invalid." },"innererror":{ "message":"application.ApplicationName : The ApplicationName field is required.\r\n","type":"","stacktrace":"" } } } 

My submit method is as follows:

  public async Task<IHttpActionResult> Post(Application application) { if (!ModelState.IsValid) { return BadRequest(ModelState); } db.Applications.Add(application); await db.SaveChangesAsync(); return Created(application); } 

I even tried to abstract this on ActionFilterAttribute, but still the same result

 public class ValidateModelAttribute : ActionFilterAttribute { public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext actionContext) { if (actionContext.ModelState.IsValid == false) { var modelState = actionContext.ModelState; if (!modelState.IsValid) actionContext.Response = actionContext.Request .CreateErrorResponse(HttpStatusCode.BadRequest, modelState); } } } 

My WebApi launch method has the following configuration:

 public static class WebApiConfig { public static void Register(HttpConfiguration config) { config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); ODataConventionModelBuilder builder = new ODataConventionModelBuilder(); builder.EntitySet<Application>("DataApplications"); config.Routes.MapODataRoute("odata", "odata", builder.GetEdmModel()); config.Formatters.JsonFormatter.SerializerSettings = new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Include }; Configure(config); config.EnableQuerySupport(); // Use camel case for JSON data. config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); 

This is what I want to achieve (does not match my example above):

 { "Message": "The request is invalid.", "ModelState": { "car": [ "Required property 'Make' not found in JSON. Path '', line 1, position 57." ], "car.Make" : [ "The Make field is required." ], "car.Price": [ "The field Price must be between 0 and 200000." ] } } 

I need to return ModelState so that I can set up feedback for validation in the appropriate fields.

Any ideas that I can check / modify to make this work as I wish?

Thanks.

Update 1 - link found on asp.net

http://www.asp.net/aspnet/overview/aspnet-and-visual-studio-2012/aspnet-and-web-tools-20122-release-notes

OData Error Response Does Not Contain Model State Errors

When creating an error response using the CreateErrorResponse or HttpErrors extension methods, the error is directly mapped to the OData error response. Any model state errors in the error response do not apply to the OData error response. To save model state errors in an OData error response, use the CreateODataErrorResponse or ODataError extension method directly and add descriptions of model state errors in the OData error message.

+8
json angularjs c # validation asp.net-web-api
source share
1 answer

OData uses an ODataError as an error response. The difference between ODataError and HttpEror is that HttpError is obtained from Dictionary<string, object> , so when it is initialized using ModelStateDictionary , all model errors are set to the ModelState property. But when you query for OData, the HttpError object HttpError mapped to an ODataError , and all validation errors are combined into the InnterError property.

0
source share

All Articles