Last post Oct 02, 2020 04:50 AM by dbrosnan
Sep 28, 2020 05:45 PM|dbrosnan|LINK
In my .net core web application using MVC, I have an Error Handling Filter which fires when there is an error. For example if i try to divide by zero, the filter fires and i can do what i want with the erorr. This works well.
public class ErrorHandlingFilter : ExceptionFilterAttribute
public override void OnException(ExceptionContext context)
var exception = context.Exception;
//log your exception here
context.ExceptionHandled = true; //optional
However, I have a Controller Action that returns a view. The view is "~/views/Section/SectionsFilterGroups.cshtml". In my project this view does not exist, so it throws an error. However, the error is not firing my Exception filter above. So why does my
exception filter fire for some errors and not others?
public IActionResult SectionsFiltersGroups(int id, int SectionId, int? SectionExpandedId)
vmSectionFilterGroups model = new vmSectionFilterGroups();
PCSection s = PCSection.Get_BySectionId(SectionId);
model.SectionId = s.SectionId;
model.SectionName = s.Name;
model.SectionFilterGroupExpandedId = SectionExpandedId;
return View($"~/views/Section/SectionsFilterGroups.cshtml", model);
I would like my Exception Filter to fire for all Exceptions. How can i do this?
Sep 29, 2020 01:43 AM|bruce (sqlwork.com)|LINK
If you read the docs
you would see the filter does not handle exceptions in result processing (like a view engine)
Sep 29, 2020 02:06 AM|dbrosnan|LINK
Ok, understood, thank you.
Basically we just want to receive an email in our .net core web app when an error occurs. We used do this in our global.asax file in .net framework which used to catch all errors.
Do you have a recomendation of another solution instead of Exception filters?
Sep 29, 2020 04:05 AM|DA924|LINK
I use the below code is Startup.cs for a global exception handler in Core MVC UI project done by a ErrorController.
The example code is out on Github.
The ErrorHandlingFilter.cs is used in the WebAPI.
You can give the MVC part a try.
Sep 30, 2020 07:18 AM|nideeshm|LINK
You may try EFK. Elastic, fluent and Kibana. Kibana can give insights into the log file.
Oct 02, 2020 04:50 AM|dbrosnan|LINK
For anyone who comes across this, here is the easiest solution that i found.
In the configure Method in Services you can put "app.UseExceptionHandler("/Errors/Error");"
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
And then you create a controller called "Errors" with a method called Error like this and you get access to the error message and the stack trace.
public class ErrorsController : Controller
public async Task Error()
var context = HttpContext.Features.Get<IExceptionHandlerFeature>();
string ErrorMessage = context.Error.Message;
string StackTrace = context.Error.StackTrace;
This also captures errors when a view is not found in the response, which is the answer to my question.
Thanks to everyone for their help.