asp.net-mvc - asp.net mvc 错误处理的最佳实践

标签 asp.net-mvc error-handling

就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the help center为指导。




9年前关闭。




我正在寻找一种标准方法来处理 asp.net mvc 2.0 或 3.0 中的错误

  • 404 错误处理程序
  • Controller 范围异常错误处理程序
  • 全局范围异常错误处理程序
  • 最佳答案

    对于 Controller 范围错误,请尝试使用自定义 Exception 属性,即

    public class RedirectOnErrorAttribute : FilterAttribute, IExceptionFilter
    {
        public void OnException(ExceptionContext filterContext)
        {
    
            // Don't interfere if the exception is already handled
            if(filterContext.ExceptionHandled)
            return;
    
            //.. log exception and do appropriate redirects here
    
         }
    }
    

    然后用属性装饰 Controller ,错误处理应该是你的
    [RedirectOnError]
    public class TestController : Controller
    {
         //.. Actions etc...
    }
    

    如果错误与路由有关,则无济于事 - 即它首先找不到 Controller 。为此,请尝试 Global.asax 中的应用程序错误处理程序,即
     protected void Application_Error(object sender, EventArgs e)
     {
          //.. perhaps direct to a custom error page is here
     }
    

    我不知道这是否是“最佳实践”。行得通。

    关于asp.net-mvc - asp.net mvc 错误处理的最佳实践,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4523831/

    相关文章:

    c# - 元素的 JQuery Mobile 渲染层次结构

    c# - 如何向 ASP.NET WebAPI Controller 添加自定义方法?

    http - 我应该使用什么HTTP错误代码来未经授权访问 protected 图像?

    grails - 通过重定向呈现命令验证错误

    Python 仅在范围内时才输入除外

    jquery - 如何记住 Kendo Grid 中第二次请求的过滤器值?

    asp.net-mvc - Bower、Grunt 和 Yeoman 如何适应 Visual Studio .NET 工作流?

    asp.net-mvc - SquishIt MVC - Debug模式,不刷新

    java - 异常处理和 IO

    go - 单个 defer func() 可以被其他函数共享吗?