2
votes

We are using WebApi v2.1 and validating ModelState via a filter applied in the WebApiConfig class.

Fields specified as required are not listed on the error message when we run on the server (Win Server 2008R2), but they work perfectly when we run locally (on IISExpress).

The request is correctly rejected locally and on the server, but the server response does not show the missing fields.

For Example: Given a Local request that lacks the required abbreviation and issuerName fields, the response shows as expected:

{ "message": "The request is invalid.", "modelState": { "value": [ "Required property 'abbreviation' not found in JSON. Path '', line 18, position 2.", "Required property 'issuerName' not found in JSON. Path '', line 18, position 2." ] }

When the same request is sent to the server, the response shows:

{ "message": "The request is invalid.", "modelState": { "value": [ "An error has occurred.", "An error has occurred." ] } }

Our filter is the following:

public class ValidateModelStateAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext actionContext)
{
if (!actionContext.ModelState.IsValid)
{
actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.BadRequest, actionContext.ModelState);
} } }

Our data model class is decorated with the DataContract attribute, and the required fields are attributed like so:

    [DataMember(IsRequired=true)]        
    public string IssuerName
1

1 Answers

4
votes

The server is more restrictive about sending errors down ot the client. Try setting the IncludeErrorDetails flag on your httpconfiguration to verify that this is the underlying issue.

In general though turning this flag on is not the best idea, and you will want to serialize the errors down differently.

For more info: http://blogs.msdn.com/b/youssefm/archive/2012/06/28/error-handling-in-asp-net-webapi.aspx