Exclude a type from model validation (example DbGeography) to avoid InsufficientExecutionStackException
Whether this issue is a bug or a limitation of Web API, I do not know, but here's my workaround:
First, we need to subclass the DefaultBodyModelValidator
and override the ShouldValidateType
method.
public class CustomBodyModelValidator : DefaultBodyModelValidator
{
public override bool ShouldValidateType(Type type)
{
return type!= typeof(DbGeography) && base.ShouldValidateType(type);
}
}
Now in global.asax's Application_Start
method, add
GlobalConfiguration.Configuration.Services.Replace(typeof(IBodyModelValidator), new CustomBodyModelValidator());
and that's it. Shallow validation will now be performed on the DbGeography
type instances and everything binds nicely.
The answer by joelmdev lead me in the right direction, but with my WebApi configuration in MVC and WebApi 5.2.3 the new validator would not get called when placed in Global.asax.
The solution was to put it in my WebApiConfig.Register method with the other WebApi routes:
config.Services.Replace(typeof(IBodyModelValidator), new CustomBodyModelValidator());