-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAppExceptionHandler.cs
More file actions
37 lines (34 loc) · 1.24 KB
/
AppExceptionHandler.cs
File metadata and controls
37 lines (34 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
using asp_net_core_filters.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
namespace asp_net_core_filters.Filters
{
public class AppExceptionHandler : IExceptionFilter
{
private readonly IModelMetadataProvider _modelMetadataProvider;
public AppExceptionHandler(
IModelMetadataProvider modelMetadataProvider)
{
_modelMetadataProvider = modelMetadataProvider;
}
public void OnException(ExceptionContext context)
{
ErrorViewModel errorViewModel = new ErrorViewModel();
errorViewModel.ErrorMessage = context.Exception.Message;
errorViewModel.Source = context.Exception.StackTrace;
ViewResult errorViewResult = new ViewResult
{
ViewName = "error",
ViewData = new ViewDataDictionary(_modelMetadataProvider,
context.ModelState)
{
Model = errorViewModel
}
};
context.ExceptionHandled = true;
context.Result = errorViewResult;
}
}
}