The problem is that in ApiController ModelState.IsValid is always true if I use .rsx file (Resources) to provide custom error message.
Here is my model:
public class LoginModel
{
public string Email { get; set; }
[Required]
[MinLength(5)]
public string Password { get; set; }
}
Method in ApiController:
[HttpPost]
[ModelValidationFilter]
public void Post(LoginModel model)
{
var a = ModelState.IsValid;
}
And the filter:
public class ModelValidationFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext actionContext)
{
if (actionContext.ModelState.IsValid == false)
{
actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.BadRequest, actionContext.ModelState);
}
}
}
I'm sending this in POST request:
{ Email: "asd@asd.com", Password: "a" }
ModelState.IsValid is false and the response is as expected:
{
"Message": "The request is invalid.",
"ModelState":
{
"model.Password":
[
"The field Password must be a string or array type with a minimum length of '5'."
]
}
}
But if I use the resources (configured as Public and Embedded Resource build action) in validation attributes:
public class LoginModel
{
public string Email { get; set; }
[Required]
[MinLength(5, ErrorMessageResourceName = "Test", ErrorMessageResourceType = typeof(Resources.Localization))]
public string Password { get; set; }
}
('Test' key holds just 'Test' string value) ModelState.IsValid is true.
Resources class is visible, and resharper correctly validates string provided in ErrorMessageResourceName.