加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – asp.net mvc错误处理的最佳做法

发布时间:2020-12-15 22:59:13 所属栏目:asp.Net 来源:网络整理
导读:我正在寻找一个标准的方法来处理asp.net mvc 2.0或3.0中的错误 404错误处理程序 控制器范围异常错误处理程序 全局范围异常错误处理程序 感谢所有 解决方法 对于控制器范围错误,尝试使用自定义异常属性,即 public class RedirectOnErrorAttribute : FilterAtt
我正在寻找一个标准的方法来处理asp.net mvc 2.0或3.0中的错误

> 404错误处理程序
>控制器范围异常错误处理程序
>全局范围异常错误处理程序

感谢所有

解决方法

对于控制器范围错误,尝试使用自定义异常属性,即
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

     }
}

然后用属性装饰控制器,错误处理应该是你的

[RedirectOnError]
public class TestController : Controller
{
     //.. Actions etc...
}

如果错误是通过路由,就不会有帮助 – 即它首先找不到控制器.为此,请尝试Global.asax中的应用程序错误处理程序,即

protected void Application_Error(object sender,EventArgs e)
 {
      //.. perhaps direct to a custom error page is here
 }

我不知道这是否是最佳实践.工作.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读