MVC Override OnException Error: No suitable method found to override
MVC Override OnException Error: No suitable method found to override
我正在尝试覆盖 Global.asax 中的 OnException 以处理错误和写入日志。我不确定哪一部分是错误的,每当我重建我的解决方案时,我都会收到错误 "MyApp.MvcApplication.OnException(System.Web.Mvc.ExceptionContext)': no suitable method found to override"。
这是我在 Application_Start()
中的代码
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
AuthConfig.RegisterAuth();
}
这是我在 OnException()
中的代码
protected override void OnException(ExceptionContext context)
{
Exception ex = context.Exception;
if (!string.IsNullOrEmpty(ex.Message) ||
!string.IsNullOrEmpty(ex.Source.ToString()) ||
!string.IsNullOrEmpty(ex.StackTrace))
{
WriteLog(ex.Message.ToString(), ex.StackTrace.ToString(), ex.Source.ToString(), "0");
context.Result = new ViewResult
{
ViewName = String.Format("~/ErrorPage/ErrorPage?message={0}&stack={1}&source={2}", HttpUtility.UrlEncode(ex.Message), HttpUtility.UrlEncode(ex.StackTrace), HttpUtility.UrlEncode(ex.Source))
};
}
context.ExceptionHandled = true;
}
WriteLog() 函数在其他应用程序中进行了测试,我认为它没有任何问题,我什至尝试过:
protected override void OnException(ExceptionContext context) {
Exception ex = context.Exception;
context.Result = new ViewResult
{
ViewName = "~/Shared/Error.cshtml";
};
context.ExceptionHandled = true;
}
但没有任何效果。错误只是留在那里。
怎么会出现这样的问题,我该如何解决?我阅读了很多关于此的教程,我不认为我拼错了 OnException()。
请帮忙。谢谢
全局asax中没有方法OnException,该方法属于controllers.to全局asax中处理错误使用方法Application_Error(object sender, EventArgs e )
Global.asax
里面没有OnException
你有两种方法:
创建您自己的 HandleErrorAttribute
并在 FilterConfig.cs
中注册
public class HandleExceptionsAttribute : HandleErrorAttribute
{
public override void OnException(ExceptionContext filterContext)
{
(...)
}
}
FilterConfig.cs:
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleExceptionsAttribute());
(...)
}
或者,如果您有一个 BaseController
继承自所有控制器,请覆盖 OnException
方法。
PS:我会选择过滤器。
我正在尝试覆盖 Global.asax 中的 OnException 以处理错误和写入日志。我不确定哪一部分是错误的,每当我重建我的解决方案时,我都会收到错误 "MyApp.MvcApplication.OnException(System.Web.Mvc.ExceptionContext)': no suitable method found to override"。
这是我在 Application_Start()
中的代码 protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
AuthConfig.RegisterAuth();
}
这是我在 OnException()
中的代码protected override void OnException(ExceptionContext context)
{
Exception ex = context.Exception;
if (!string.IsNullOrEmpty(ex.Message) ||
!string.IsNullOrEmpty(ex.Source.ToString()) ||
!string.IsNullOrEmpty(ex.StackTrace))
{
WriteLog(ex.Message.ToString(), ex.StackTrace.ToString(), ex.Source.ToString(), "0");
context.Result = new ViewResult
{
ViewName = String.Format("~/ErrorPage/ErrorPage?message={0}&stack={1}&source={2}", HttpUtility.UrlEncode(ex.Message), HttpUtility.UrlEncode(ex.StackTrace), HttpUtility.UrlEncode(ex.Source))
};
}
context.ExceptionHandled = true;
}
WriteLog() 函数在其他应用程序中进行了测试,我认为它没有任何问题,我什至尝试过:
protected override void OnException(ExceptionContext context) {
Exception ex = context.Exception;
context.Result = new ViewResult
{
ViewName = "~/Shared/Error.cshtml";
};
context.ExceptionHandled = true;
}
但没有任何效果。错误只是留在那里。
怎么会出现这样的问题,我该如何解决?我阅读了很多关于此的教程,我不认为我拼错了 OnException()。
请帮忙。谢谢
全局asax中没有方法OnException,该方法属于controllers.to全局asax中处理错误使用方法Application_Error(object sender, EventArgs e )
Global.asax
OnException
你有两种方法:
创建您自己的 HandleErrorAttribute
并在 FilterConfig.cs
public class HandleExceptionsAttribute : HandleErrorAttribute
{
public override void OnException(ExceptionContext filterContext)
{
(...)
}
}
FilterConfig.cs:
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleExceptionsAttribute());
(...)
}
或者,如果您有一个 BaseController
继承自所有控制器,请覆盖 OnException
方法。
PS:我会选择过滤器。