locked
.Net Core Error Handling Not Firing On View Not Found RRS feed

  • Question

  • User-960459514 posted

    Hi All,

    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();
    
                CommonModel.SetCommonFieldsByUserId(model, id);
    
                PCSection s = PCSection.Get_BySectionId(SectionId);
    
                model.SectionId = s.SectionId;
                model.SectionName = s.Name;
    
                SectionsFiltersGroups_PopulateSectionFilterGroupItems(model, SectionId);
    
                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?

    Monday, September 28, 2020 5:45 PM

Answers

  • User-960459514 posted

    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");"

           if (env.IsDevelopment())
                {
    
                    app.UseDeveloperExceptionPage();
    
                }
                else
                {
    
                    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.
                    app.UseHsts();
                }

    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. 

    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Diagnostics;
    using Microsoft.AspNetCore.Mvc;
    
    
    namespace ProjectName.Controllers
    {
        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. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 2, 2020 4:50 AM

All replies

  • User-474980206 posted

    If you read the docs

      https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/filters?view=aspnetcore-3.1#exception-filters

    you would see the filter does not handle exceptions in result processing (like a view engine)

    Tuesday, September 29, 2020 1:43 AM
  • User-960459514 posted

    Hi,

    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?

    Thank you

    David

    Tuesday, September 29, 2020 2:06 AM
  • User1120430333 posted

    I use the below code is Startup.cs for a global exception handler in Core MVC UI project done by a ErrorController.

     app.UseExceptionHandler("/Home/Error");

    The example code is out on Github.

    https://github.com/darnold924/PublishingCompany

    The ErrorHandlingFilter.cs is used in the WebAPI.

    https://stackify.com/csharp-catch-all-exceptions/

    You can give the MVC part a try.

    Tuesday, September 29, 2020 4:05 AM
  • User585649674 posted

    You may try EFK. Elastic, fluent and Kibana. Kibana can give insights into the log file.

    Wednesday, September 30, 2020 7:18 AM
  • User-960459514 posted

    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");"

           if (env.IsDevelopment())
                {
    
                    app.UseDeveloperExceptionPage();
    
                }
                else
                {
    
                    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.
                    app.UseHsts();
                }

    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. 

    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Diagnostics;
    using Microsoft.AspNetCore.Mvc;
    
    
    namespace ProjectName.Controllers
    {
        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. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 2, 2020 4:50 AM