@ -1,7 +1,11 @@
using System.Threading.Tasks ;
using System.Linq ;
using System.Threading.Tasks ;
using Microsoft.AspNetCore.Mvc.Abstractions ;
using Microsoft.AspNetCore.Mvc.Abstractions ;
using Microsoft.AspNetCore.Mvc.Filters ;
using Microsoft.AspNetCore.Mvc.Filters ;
using Microsoft.Extensions.Options ;
using Volo.Abp.DependencyInjection ;
using Volo.Abp.DependencyInjection ;
using Volo.Abp.Reflection ;
using Volo.Abp.Validation ;
namespace Volo.Abp.AspNetCore.Mvc.Validation
namespace Volo.Abp.AspNetCore.Mvc.Validation
{
{
@ -9,8 +13,6 @@ namespace Volo.Abp.AspNetCore.Mvc.Validation
{
{
public async Task OnActionExecutionAsync ( ActionExecutingContext context , ActionExecutionDelegate next )
public async Task OnActionExecutionAsync ( ActionExecutingContext context , ActionExecutionDelegate next )
{
{
//TODO: Configuration to disable validation for controllers..?
if ( ! context . ActionDescriptor . IsControllerAction ( ) | |
if ( ! context . ActionDescriptor . IsControllerAction ( ) | |
! context . ActionDescriptor . HasObjectResult ( ) )
! context . ActionDescriptor . HasObjectResult ( ) )
{
{
@ -18,6 +20,44 @@ namespace Volo.Abp.AspNetCore.Mvc.Validation
return ;
return ;
}
}
if ( ! context . GetRequiredService < IOptions < AbpAspNetCoreMvcOptions > > ( ) . Value . AutoModelValidation )
{
await next ( ) ;
return ;
}
if ( ReflectionHelper . GetSingleAttributeOfMemberOrDeclaringTypeOrDefault < DisableValidationAttribute > ( context . ActionDescriptor . GetMethodInfo ( ) ) ! = null )
{
await next ( ) ;
return ;
}
if ( ReflectionHelper . GetSingleAttributeOfMemberOrDeclaringTypeOrDefault < DisableValidationAttribute > ( context . Controller . GetType ( ) ) ! = null )
{
await next ( ) ;
return ;
}
if ( context . ActionDescriptor . GetMethodInfo ( ) . DeclaringType ! = context . Controller . GetType ( ) )
{
var baseMethod = context . ActionDescriptor . GetMethodInfo ( ) ;
var overrideMethod = context . Controller . GetType ( ) . GetMethods ( ) . FirstOrDefault ( x = >
x . DeclaringType = = context . Controller . GetType ( ) & &
x . Name = = baseMethod . Name & &
x . ReturnType = = baseMethod . ReturnType & &
x . GetParameters ( ) . Select ( p = > p . ToString ( ) ) . SequenceEqual ( baseMethod . GetParameters ( ) . Select ( p = > p . ToString ( ) ) ) ) ;
if ( overrideMethod ! = null )
{
if ( ReflectionHelper . GetSingleAttributeOfMemberOrDeclaringTypeOrDefault < DisableValidationAttribute > ( overrideMethod ) ! = null )
{
await next ( ) ;
return ;
}
}
}
context . GetRequiredService < IModelStateValidator > ( ) . Validate ( context . ModelState ) ;
context . GetRequiredService < IModelStateValidator > ( ) . Validate ( context . ModelState ) ;
await next ( ) ;
await next ( ) ;
}
}