Search code examples
sweetalertaspnetboilerplate

Is it possible to send userfriendlyexception from controller with sweet alert


I want to return sweet alerts from controller to warn a user about customized errors according to my needs. I could not figure it out in controller. Basically I throw userfriendlyexception from my action but errors are shown in another way. I have referenced abp.UI in the controllers. I have changed the layout's html and css, but still including sweet alert's js and css files and it's working in the service level as a proof.

Thanks in advance.


Solution

  • ASP.NET Boilerplate has different behaviours based on return type of your Controller's Action:

    • If action returns JsonResult or another object (or their async Task<T> versions) then it returns an AjaxResponse and client side can handle it and show a sweetalert message. So, if you call an action with AJAX and not returning HTML then explicitly use JsonResult of other POCO object as return value of your action.

    • If action's return type is ActionResult, ViewResult or PartialViewResult (or their async Task<T> versions) then it returns the Error view.

    See documentation for more: https://aspnetboilerplate.com/Pages/Documents/AspNet-Core#exception-filter