3

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

1

2 Answers 2

5

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)

Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.