0

I have created a CRUD application using ASP.NET MVC and Syncfusion components. When it gets errors on the database side, I use exception to handle them. After publishing and running the project on IIS, I have no problem running on the server side but client side doesn't receive the exception message correctly.

// ASP.NET MVC controller
try
{
   db.SaveChanges();
}
catch (Exception)
{
   throw new Exception("Foreign key detected");
}

Object received in the view in server side contains "Foreign key detected" as part of the HTML, but when running on client side default view is passed.

// View script
function failure(args) {
    var ER1 = args.error[0].error.responseText.split("Exception:")[1].split('<br>')[0];
    alert(ER1);
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
gangroid1991
  • 13
  • 1
  • 4
  • A couple of notes here, 1: If you are running your server in non-developer mode, depending on the version of .NET you might not even getting the exception in production. 2: Because of #1 I recommend using a custom object for returning the exceptions to the client if you have to do it. – NavidM Aug 01 '22 at 05:10
  • @NavidM Do you have any suggestion for passing exception from controller to view? – gangroid1991 Aug 01 '22 at 05:15
  • I don't have enough information about your controller method, but in general you can do something like `return StatusCode(500, "Database: Foreign key detected");` or fancier serializing the exception to json and return it. – NavidM Aug 01 '22 at 05:19

0 Answers0