MyTested.AspNetCore.Mvc
MyTested.AspNetCore.Mvc copied to clipboard
Add option to execute action filters
Example test:
MyController<HomeController>
.Instance()
.WithFilters(filters => filters
.OnActionExecuting()
.OnActionExecuted())
.Calling(c => c.Index())
.ShouldReturn()
.Ok();
This is great, would love to see that as I use filters to validate ModelState
@manuelids Could you please provide the code you use in the action filter to validate the ModelState? Thank you!
@manuelids Could you please provide the code you use in the action filter to validate the ModelState? Thank you!
public class ValidationActionFilter : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext context)
{
if (!context.ModelState.IsValid)
{
context.Result = new ValidationFailedResult(context.ModelState);
}
}
}
This validationActionFilter returns a custom Object result that returns a BadRequest and displays all the validation errors per field.
Filter is registered at Startup
services.AddMvc().AddMvcOptions(o =>
{
o.Filters.Add(new ValidationActionFilter());
}).SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
Thanks much!