Base solution for your next web application
Open Closed

ValidationErrors not to show detail message #3378


User avatar
0
nancyphan created

Could you help me. I using template ASP.Net Boilerplate framework. I config in web module like that:

public override void PreInitialize()
        {
            //Enable database based localization
            Configuration.Modules.Zero().LanguageManagement.EnableDbLocalization();

            //Configure navigation/menu
            Configuration.Navigation.Providers.Add<LabourXchangeAPINavigationProvider>();
            Configuration.Modules.AbpWeb().AntiForgery.IsEnabled = false;
            Configuration.Modules.AbpWebCommon().SendAllExceptionsToClients = true;
            //Configuration.Modules.AbpMvc().IsValidationEnabledForControllers = true;
            //Configure Hangfire - ENABLE TO USE HANGFIRE INSTEAD OF DEFAULT JOB MANAGER
            //Configuration.BackgroundJobs.UseHangfire(configuration =>
            //{
            //    configuration.GlobalConfiguration.UseSqlServerStorage("Default");
            //});
            var cors = new EnableCorsAttribute("*", "*", "*");
            Configuration.Modules.AbpWebApi().HttpConfiguration.EnableCors(cors);

        };

and web.config:

<customErrors mode="Off" />

I created user and it is invalid email but it not to show detail message of validation errors. It show the message below:

Abp.Runtime.Validation.AbpValidationException: Method arguments are not valid! See ValidationErrors for details.
   at Abp.Runtime.Validation.Interception.MethodInvocationValidator.Validate() in D:\Github\aspnetboilerplate\src\Abp\Runtime\Validation\Interception\MethodInvocationValidator.cs:line 94
   at Abp.WebApi.Validation.AbpApiValidationFilter.<ExecuteActionFilterAsync>d__5.MoveNext() in D:\Github\aspnetboilerplate\src\Abp.Web.Api\WebApi\Validation\AbpApiValidationFilter.cs:line 46
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at Abp.WebApi.Auditing.AbpApiAuditFilter.&lt;ExecuteActionFilterAsync&gt;d__4.MoveNext() in D:\Github\aspnetboilerplate\src\Abp.Web.Api\WebApi\Auditing\AbpApiAuditFilter.cs:line 47
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Abp.WebApi.Auditing.AbpApiAuditFilter.&lt;ExecuteActionFilterAsync&gt;d__4.MoveNext() in D:\Github\aspnetboilerplate\src\Abp.Web.Api\WebApi\Auditing\AbpApiAuditFilter.cs:line 53
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__2.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at Abp.WebApi.Security.AntiForgery.AbpAntiForgeryApiFilter.&lt;ExecuteAuthorizationFilterAsync&gt;d__10.MoveNext() in D:\Github\aspnetboilerplate\src\Abp.Web.Api\WebApi\Security\AntiForgery\AbpAntiForgeryApiFilter.cs:line 51
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at Abp.WebApi.Authorization.AbpApiAuthorizeFilter.<ExecuteAuthorizationFilterAsync>d__7.MoveNext() in D:\Github\aspnetboilerplate\src\Abp.Web.Api\WebApi\Authorization\AbpApiAuthorizeFilter.cs:line 69
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at System.Web.Http.Controllers.AuthenticationFilterResult.&lt;ExecuteAsync&gt;d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at System.Web.Http.Controllers.ExceptionFilterResult.<ExecuteAsync>d__0.MoveNext()
WARN  2017-06-12 21:32:29,143 [71   ] nHandling.AbpApiExceptionFilterAttribute - There are 1 validation errors:
WARN  2017-06-12 21:32:29,143 [71   ] nHandling.AbpApiExceptionFilterAttribute - The EmailAddress field is not a valid e-mail address. (input.EmailAddress)

Please help me to fix this problem :( Many thanks in advance


1 Answer(s)
  • User Avatar
    0
    gpcaretti created

    You passed wrong values to your service method that does not respect the rules you defined.

    On server side you can do more things:

    1- trace your call and catch the AbpValidationException

    try {
     ...
    } catch(AbpValidationException ex) {
                var string = ex.ValidationErrors
                        .Select(err => err.ToString())
                        .Aggregate(string.Empty, (current, next) => string.Format("{0}\n{1}", current, next));
    }
    

    Another solution is to edit your Dto input and implement the ICustomValidate.

    For example:

    public class AddBidInput : ICustomValidate, IMyDto {
            public long AuctionId { get; set; }
            ...
    
           public virtual void AddValidationErrors(CustomValidationContext context) {
                var results = context.Results;
    
                if (AuctionId <= 0) {
                   results.Add(new ValidationResult("auction id cannot be <= 0", new[] { "AuctionId" }));
                }
          }
    }