簡體   English   中英

如何在 asp.net 內核的基礎 controller 中處理 On Exception?

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

在此處輸入圖像描述我是開發新手,我正在研究將 asp.net 應用程序轉換為 asp.net 內核的項目。 但是我在 on exception 方法上面臨一些挑戰。 為此,我需要您的幫助和指導。 拋出異常錯誤,因為沒有找到合適的方法覆蓋。 下面是我的代碼。

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

在此處輸入圖像描述

非常感謝朋友們的指導。 希望該解決方案也對您有所幫助。

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

您收到的錯誤是因為父 class 沒有定義名為OnException(ExceptionContext filterContext)的方法

You can see this from the documentation that changing from the MVC (.NET Framework) Controller ( https://docs.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://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.controller?view=aspnetcore-3.1 ) no such method is定義。

為了在 .Net Core 中具有類似的功能,請考慮將上面的 OnException 邏輯移動到ExceptionFilter ( https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/filters?view=aspnetcore-3.1 #異常過濾器

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM