Shanaaz Naushard
Shanaaz Naushard

Reputation: 33

How to handle On Exception in the base controller of asp.net core?

enter image description hereI am new to development and I am working on a project to convert the asp.net application to asp.net core. But I am facing a few challenges on the on exception method. I need your assistance and guidance for this cause. The on exception error is thrown as there is not suitable method found over ride. Below is my code.

protected override void OnException(ExceptionContext filterContext)
        {
            var urlReferrerAbsolutePath = HttpContext.Request.GetDisplayUrl();

            var canvasName = urlReferrerAbsolutePath.Split('-').Last();
            canvasName = canvasName.Replace("%20", " ");

            ec_log log = new ec_log()
            {
                Date = DateTime.Now.ToUniversalTime(),
                ControllerName = filterContext.RouteData.Values["controller"].ToString(),
                ActionName = filterContext.RouteData.Values["action"].ToString(),
                Type = Share.MESSAGE_TYPE_ERROR,
                LogginOn = Share.MESSAGE_TYPE_EXCEPTION,
                LoggedInUserId = LoggedInId,
                LoggedInUserName = Share.LoggedInMembersFirstName,
                Exception = (filterContext.Exception.InnerException != null ?
                                 filterContext.Exception.InnerException.ToString()
                                 : "") + " " + filterContext.Exception.Message,
                System = _systemName,
                CompanyID = Share.LoggedInMembersCompanyID,
                Url = filterContext.HttpContext.Request.GetDisplayUrl(),
                CanvasName = canvasName,
                StackTrace = filterContext.Exception.StackTrace != null ? filterContext.Exception.StackTrace : ""
            };
            new LogModel(_configuration).InsertLog(log);
            if (filterContext.ExceptionHandled)
            {
                return;
            }
            filterContext.Result = new ViewResult
            {
                ViewName = "~/Views/Shared/Error.aspx"
            };
            filterContext.ExceptionHandled = true;
        }

enter image description here

Upvotes: 1

Views: 4241

Answers (2)

Shanaaz Naushard
Shanaaz Naushard

Reputation: 33

Thanks a lot for your guidance my friends. Hope the solution will be helpful for you as well.

public virtual void OnException (Microsoft.AspNetCore.Mvc.Filters.ExceptionContext filtercontext);

Upvotes: 2

Francois du Plessis
Francois du Plessis

Reputation: 2581

The error you are getting is because the parent class does not define the method called OnException(ExceptionContext filterContext)

You can see this from the documentation that changing from the MVC (.NET Framework) Controller (https://learn.microsoft.com/en-us/dotnet/api/system.web.mvc.controller?view=aspnet-mvc-5.2) the class contains the method but for .NET Core Controller (https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.controller?view=aspnetcore-3.1) no such method is defined.

In order to have similar functionality in .Net Core, consider moving the OnException logic above into an ExceptionFilter (https://learn.microsoft.com/en-us/aspnet/core/mvc/controllers/filters?view=aspnetcore-3.1#exception-filters)

Upvotes: 4

Related Questions