2

我正在处理我的控制器中的错误,并且我[CustomErrorHandleAttribute]已经写了当我的操作出现异常时该怎么做。即使我的代码中没有错误,它也会重定向到 customerrorhandle 并抛出错误。我无法找到它为什么这样做的错误。

这是我的代码:

    namespace ExceptionHandlingInMVC.Controllers
    {
    [CustomHandleError]
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public object Index()
        {
            try
            {
                ViewData["Title"] = "Home Page";
                ViewData["Message"] = "Current time is:" + DateTime.Now.ToLongTimeString();
                var x = 10;
                var y = 10;
                var result = x / y;
                ViewData["Result"] = result;
                return View();
            }
            catch (Exception e)
            {

                throw e;
            }

        }

        [CustomHandleError]
        public object About()
        {
            ViewData["Title"] = "About Page";
            return View();
        }
    }

    public class ErrorPresentation
    {
        public String ErrorMessage { get; set; }
        public Exception TheException { get; set; }
        public Boolean ShowMessage { get; set; }
        public Boolean ShowLink { get; set; }


    }

  }

我写的CustomHandleErrorAttribute:

    namespace ExceptionHandlingInMVC
   {

    /// <summary>
    /// This attribute (AOP) filter is used to override the Error handling and make sure that all erros are recorded in the event logs, so that they can in turn be picked up by 
    /// our SIEM tool  so that we a) stop customers seing a bad error message and b) we are capturing all the events that happen and c) improives security for 
    /// by preventing a hacker from seing s=details of how our application is put together
    /// </summary>
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
    public sealed class CustomHandleErrorAttribute : ActionFilterAttribute
    {
        /// <summary>
        /// This event is called when the action is called i.e. an error has just occured
        /// </summary>
        /// <param name="filterContext"></param>
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            try
            {
                // Bail if we can't do anything; app will crash.
                if (filterContext == null)
                    return;

                // since we're handling this, log to ELMAH(Error logging modules and handler)
                var ex = filterContext.Exception ?? new Exception("No further information exists.");
                WriteToEventLog(ex);

                filterContext.ExceptionHandled = true;
                var data = new ErrorPresentation
                {
                    ErrorMessage = HttpUtility.HtmlEncode(ex.Message),
                    TheException = ex,
                    ShowMessage = filterContext.Exception != null,
                    ShowLink = false
                };

                filterContext.Result = new ViewResult
                {
                    ViewName = "~/Views/Home/ErrorPage.aspx"
                };
            }
            catch (Exception exception)
            {

                throw;
            }

        }

        /// <summary>
        /// This method writes the exception to the event log we have specified in the web.config or the app.config
        /// </summary>
        /// <param name="exception"></param>
        public void WriteToEventLog(Exception exception)
        {
            // pick up which machine we are on, this will already be set for all websites
            var machineName = ConfigurationManager.AppSettings["MachineName"];

            // PIck up the eventlog we are going to write to
            var eventLogName = ConfigurationManager.AppSettings["EventLogName"];

            EventLog.WriteEntry("abc", exception.Message, EventLogEntryType.Error);

        }
    }
}
4

3 回答 3

2

试试这个:

public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        try
        {
            // Bail if we can't do anything; app will crash.
            if (filterContext == null)
                return;

            // since we're handling this, log to ELMAH(Error logging modules and handler)
            if (filterContext.Exception == null || filterContext.ExceptionHandled)
            {
                var ex = filterContext.Exception ?? new Exception("No further information exists.");
                this.WriteToEventLog(ex);
                return;
            };

            filterContext.ExceptionHandled = true;
            var data = new ErrorPresentation
            {
                ErrorMessage = HttpUtility.HtmlEncode(ex.Message),
                TheException = ex,
                ShowMessage = filterContext.Exception != null,
                ShowLink = false
            };

            filterContext.Result = new ViewResult
            {
                ViewName = "~/Views/Home/ErrorPage.aspx"
            };
        }
        catch (Exception exception)
        {

            throw;
        }

    }

如果没有异常,则需要返回,因为此属性每次都会触发,而不仅仅是在出现错误时触发。

更新:我建议您在 global.asax 中编写以下代码:

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new CustomErrorHandle());
    }

为所有操作触发此属性。所以你不需要为任何动作写属性。

于 2013-02-28T12:53:00.850 回答
2

你真的应该通过覆盖Application_Errorglobal.asax 来执行你的错误处理。这样您就可以确定您的代码只会在发生错误时执行。UsingOnActionExecuted意味着无论是否抛出错误,您的代码都将执行。

这是功能:

void Application_Error(object sender, EventArgs e)
{
    //do your stuff here
}
于 2013-02-28T16:02:31.717 回答
0

仅在出现错误并且我正在写入事件日志时才应触发事件:

在我的 global.asax 中,我添加了以下代码:

    /// <summary>
    /// Managing errors from a single location 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    void Application_Error(object sender, EventArgs e)
    {
        // 1. Get the last error raised
        var error = Server.GetLastError();

        //2. Get the error code to respond with
        var code = (error is HttpException) ? (error as HttpException).GetHttpCode() : 500;

        //3.. Log the error ( I am ignoring 404 error)
        if (code != 404)
        {
            // Write error details to eventlog
            WriteToEventLog(error);
        }

        //4. Clear the response stream
        Response.Clear();

        //5. Clear the server error
        Server.ClearError();

        //6. Render the Error handling controller without a redirect
        string path = Request.Path;
        Context.RewritePath(string.Format("~/Home/Error",code),false);
        IHttpHandler httpHandler = new MvcHttpHandler();
        httpHandler.ProcessRequest(Context);
        Context.RewritePath(path,false);
    }

     /// <summary>
    /// This method writes the exception to the event log we have specified in the web.config or the app.config
    /// </summary>
    /// <param name="exception"></param>
    public void WriteToEventLog(Exception exception)
    {
        EventLog.WriteEntry("abc", exception.Message, EventLogEntryType.Error);
    }
于 2013-02-28T15:57:22.743 回答