Пример #1
0
        public static IServiceCollection ConfigureApiBehavior(this IServiceCollection services)
        {
            services.Configure <ApiBehaviorOptions>(options =>
            {
                options.InvalidModelStateResponseFactory = context =>
                {
                    var descriptor = context.ActionDescriptor.CastTo <ControllerActionDescriptor>();

                    if (descriptor.MethodInfo.CustomAttributes
                        .Any(x => x.AttributeType == typeof(DontWrapInvalidModelStateAttribute))
                        //todo test for controller!
                        || descriptor.ControllerTypeInfo.CustomAttributes
                        .Any(x => x.AttributeType == typeof(DontWrapInvalidModelStateAttribute)))
                    {
                        var errors = context.ModelState.ToDictionary();

                        LogFactory.GetLogger(descriptor.ControllerTypeInfo)
                        .LogError("Validation error: {Values}", errors);

                        var result = new BadRequestObjectResult(context.ModelState);
                        return(result);
                    }

                    return(ControllerHelpers.BadRequest(context.ModelState).ToActionResult());
                };
            });

            return(services);
        }
 protected ActionResult WlBadRequest()
 {
     return(ControllerHelpers.BadRequest(ModelState).ToActionResult());
 }