diff --git a/src/Serenity.Net.Web/Mvc/HandleControllerExceptionAttribute.cs b/src/Serenity.Net.Web/Mvc/HandleControllerExceptionAttribute.cs new file mode 100644 index 0000000000..661ef3eddc --- /dev/null +++ b/src/Serenity.Net.Web/Mvc/HandleControllerExceptionAttribute.cs @@ -0,0 +1,27 @@ +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.Filters; +using Microsoft.AspNetCore.Mvc.ModelBinding; +using Microsoft.AspNetCore.Mvc.ViewFeatures; + +namespace Serenity.Services; + +/// +/// An exception filter attribute to handle controller exceptions and return as view. +/// +public class HandleControllerExceptionAttribute : ExceptionFilterAttribute +{ + /// + public override void OnException(ExceptionContext context) + { + context.ExceptionHandled = true; + var result = context.Exception.ConvertToResponse(context.HttpContext); + context.Result = new ViewResult() + { + ViewName = "~/Views/Errors/ValidationError.cshtml", + ViewData = new ViewDataDictionary(new EmptyModelMetadataProvider(), context.ModelState) + { + Model = new ValidationError(result.Error.Code, result.Error.Message), + } + }; + } +}