A filter attribute is an ASP.NET Core attribute-based component that changes filter-pipeline behavior for selected controllers or action methods. It narrows a configuration change that would otherwise affect the whole application. section 6.2.3
ManualValidationFilterAttribute implements IActionModelConvention. During action-model construction, it finds and removes the ModelStateInvalidFilter, allowing one decorated action to handle ModelState validation manually while automatic validation remains enabled elsewhere. section 6.2.3
Use the attribute when a single action needs different validation behavior:
- Create ManualValidationFilterAttribute in the Attributes folder. section 6.2.3
- Inspect action.Filters for ModelStateInvalidFilter or the internal ModelStateInvalidFilterFactory name, then remove the matching filter. section 6.2.3
- Decorate the chosen action with [ManualValidationFilter], and restore application-wide automatic ModelState validation. section 6.2.3
Selective manual validation
Decorate DomainsController.Get with [ManualValidationFilter]. That action can inspect ModelState and construct its own ValidationProblemDetails response, while other controllers and methods retain automatic validation. section 6.2.3
Listing 6.7 ManualValidationFilterAttribute
using Microsoft.AspNetCore.Mvc.ApplicationModels;
using Microsoft.AspNetCore.Mvc.Infrastructure;
namespace MyBGList.Attributes
{
public class ManualValidationFilterAttribute
: Attribute, IActionModelConvention
{
public void Apply(ActionModel action)
{
for (var i = 0; i < action.Filters.Count; i++)
{
if (action.Filters[i] is ModelStateInvalidFilter
|| action.Filters[i].GetType().Name ==
"ModelStateInvalidFilterFactory")
{
action.Filters.RemoveAt(i);
break;
}
}
}
}
}
Inspect the complete convention implementation that removes the model-state invalid filter. section 6.2.3
[HttpGet(Name = "GetDomains")]
[ResponseCache(Location = ResponseCacheLocation.Any, Duration = 60)]
[ManualValidationFilter] #1
public async Task<ActionResult<RestDTO<Domain[]>>> Get(
[FromQuery] RequestDTO<DomainDTO> input)
See the attribute applied directly to the selected action. section 6.2.3
