In ASP.NET Web API I want to include a valid field when returning errors to the caller.
public class User
{
[Required(ErrorMessage = "ThirdPartyID is required!")]
public int ThirdPartyID { get; set; }
[Required(ErrorMessage = "Firstname is required!")]
public string Firstname { get; set; }
[Required(ErrorMessage = "Lastname is required!")]
public string Lastname { get; set; }
}
When the caller posts a user with the ThirdParyID set, and the Firstname set, but without the Lastname, I want to add the ThirdPartyID to the response. I am using the below for Model Validation.
public class ModelValidationFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext actionContext)
{
if (actionContext.ModelState.IsValid == false)
{
actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.BadRequest, actionContext.ModelState);
}
}
}
Is there something I can set on the User to indicate that the ThirdPartyID should always be included in the returned error messages?