Base solution for your next web application
Open Closed

How to avoid inbuilt framework error handling/validation #1538


User avatar
0
aniljain created

My requirement is to read data from excel file (client side) and post the JSON to server which will create enteries in one of the DB table. Before creating records I need to validate data at server side. If I add validation attribute to DTO, the framework automatically validate the object and through errors while I need them to be formatted or represented in better way. How can I handle that.

Also, I was trying the rely on Entityframework validation which will give errors if something wrong is found so I am catching DBExceptions like below and want to return errors the way I want to:

catch (Exception e) { var exc = e as DbEntityValidationException; foreach (var eve in exc.EntityValidationErrors) { var message = string.Format("For member {0} validation {1}{2} found", member.UserName, exc.EntityValidationErrors.Count(), exc.EntityValidationErrors.Count()>1? "errors":"error"); foreach (var ve in eve.ValidationErrors) { message = message + ve.ErrorMessage; errors.Add(message); } } } } return errors;

but on the UI I dont get the error results though still get the system generated error message.

Can someone please help me with this?


1 Answer(s)
  • User Avatar
    0
    hikalkan created
    Support Team

    Hi,

    • <a class="postlink" href="http://www.aspnetboilerplate.com/Pages/Documents/Validating-Data-Transfer-Objects#disabling-validation">http://www.aspnetboilerplate.com/Pages/ ... validation</a> (use [DisableValidation] attribute for a method to disable validation).
    • You can disable exception handling (<a class="postlink" href="http://www.aspnetboilerplate.com/Pages/Documents/MVC-Controllers#exception-handling--result-wrapping">http://www.aspnetboilerplate.com/Pages/ ... t-wrapping</a>) or manually try-catch exception and throw a userfriendly exception. The key point here is that: You should call CurrentUnitOfWork.SaveChanges() in order to immediately save changes (and thus make the EF validation).