How to find in which controller/action an error oc

2019-02-04 06:22发布

I'm logging all errors occuring in my OnException method.

How to find in which controller/action an error occurred?

3条回答
再贱就再见
2楼-- · 2019-02-04 06:35

As Charlino implied, the values are available from the ExceptionContext parameter:

protected override void OnException(ExceptionContext filterContext)
{
    var controllerName = filterContext.RouteData.Values["controller"];
    var actionName = filterContext.RouteData.Values["action"];

    // ...

    base.OnException(filterContext);
}
查看更多
女痞
3楼-- · 2019-02-04 06:39

Check the exception's call stack.

For example:

var actionMethod = new StackTrace(exception)
    .GetFrames().FirstOrDefault(f => 
        typeof(IController).IsAssignableFrom(f.GetMethod().DeclaringType)
    ).GetMethod();
查看更多
ら.Afraid
4楼-- · 2019-02-04 06:44

Add the following method in your global.asax and put a break point on it

public void Application_Error(object sender, EventArgs e)
    {


    }

No matter where in the application an error occurs, the break point on this method will be hit. From here you can see the value for the following expression in the quick watch window and you will know what exactly was the cause of the exception that occurred

((System.Web.HttpApplication)(sender)).Context.AllErrors

This method will help, no matter where the exception occurs in your web application.

查看更多
登录 后发表回答