-
Notifications
You must be signed in to change notification settings - Fork 802
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add HandleControllerExceptionAttribute (#6853)
- Loading branch information
1 parent
1e47110
commit b456f0d
Showing
1 changed file
with
27 additions
and
0 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
src/Serenity.Net.Web/Mvc/HandleControllerExceptionAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
|
||
/// <summary> | ||
/// An exception filter attribute to handle controller exceptions and return as view. | ||
/// </summary> | ||
public class HandleControllerExceptionAttribute : ExceptionFilterAttribute | ||
{ | ||
/// <inheritdoc/> | ||
public override void OnException(ExceptionContext context) | ||
{ | ||
context.ExceptionHandled = true; | ||
var result = context.Exception.ConvertToResponse<ServiceResponse>(context.HttpContext); | ||
context.Result = new ViewResult() | ||
{ | ||
ViewName = "~/Views/Errors/ValidationError.cshtml", | ||
ViewData = new ViewDataDictionary<ValidationError>(new EmptyModelMetadataProvider(), context.ModelState) | ||
{ | ||
Model = new ValidationError(result.Error.Code, result.Error.Message), | ||
} | ||
}; | ||
} | ||
} |