异常处理是每一个系统都必须要有的功能,尤其对于Web系统而言,简单、统一的异常处理模式尤为重要,当打算使用ASP.NET MVC来做项目时,第一个数据录入页面就遇到了这个问题。

在之前的ASP.NET WebForm项目中,一般情况下都是在Application_Error事件处理器和ScriptManager_AsyncPostBackError事件处理器里面进行,在ASP.NET MVC中用这两种方法似乎都不合适了,该放在哪儿呢?总不至于在每个Action里面都放一个try{...}catch{...}吧。

在ScottGu的博客里面提到了一个类:HandleErrorAttribute,似乎是用于处理异常的,于是使用HandleErrorAttribute来做个尝试,(说明,如果使用了该类型,并且想把异常显示在自已指定的View,则必须在web.config里面的<system.web>节点加上<customErrors mode="On" />)发现HandleError的确比较好用,可以使用其View属性指定异常后跳转的页面,可以针对不同的异常类型跳到不同的异常显示View,而且也可以不跳转到异常显示View,显示到当前View,例:

[HttpPost]
[HandleError(View = "Create", ExceptionType = typeof(Exception))]
public ActionResult Create(string someParam)
{
    throw new Exception("oops...");
}

当异常发生时,页面还会跳回到Create,只是这里有点小问题,用户在页面上输入了很多东西,你提示个异常不至于把他辛辛苦苦输了半天的东西都没有了吧,把这样的项目送出去,迟早是要改回来的。

打开HandleErrorAttribute的源代码可以看其关键部分:

public virtual void OnException(ExceptionContext filterContext) {
    if (filterContext == null) {
        throw new ArgumentNullException("filterContext");
    }
    if (filterContext.IsChildAction) {
        return;
    }
 
    // If custom errors are disabled, we need to let the normal ASP.NET exception handler
    // execute so that the user can see useful debugging information.
    if (filterContext.ExceptionHandled || !filterContext.HttpContext.IsCustomErrorEnabled) {
        return;
    }
 
    Exception exception = filterContext.Exception;
 
    // If this is not an HTTP 500 (for example, if somebody throws an HTTP 404 from an action method),
    // ignore it.
    if (new HttpException(null, exception).GetHttpCode() != 500) {
        return;
    }
 
    if (!ExceptionType.IsInstanceOfType(exception)) {
        return;
    }
 
    string controllerName = (string)filterContext.RouteData.Values["controller"];
    string actionName = (string)filterContext.RouteData.Values["action"];
    HandleErrorInfo model = new HandleErrorInfo(filterContext.Exception, controllerName, actionName);
    filterContext.Result = new ViewResult {
        ViewName = View,
        MasterName = Master,
        ViewData = new ViewDataDictionary<HandleErrorInfo>(model),
        TempData = filterContext.Controller.TempData
    };
    filterContext.ExceptionHandled = true;
    filterContext.HttpContext.Response.Clear();
    filterContext.HttpContext.Response.StatusCode = 500;
 
    // Certain versions of IIS will sometimes use their own error page when
    // they detect a server error. Setting this property indicates that we
    // want it to try to render ASP.NET MVC's error page instead.
    filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
}

可以很清楚的看到,MVC实际上是使用刚才我们指定的View名称新建了一个ViewResult,然后将这个ViewResult交给了InvokeActionResult方法,最终显示给了用户。在这个过程中,新的ViewResult的ViewData被设定为HandleErrorInfo了,没有将Create上的数据放进ViewData,尽管在之后显示的Create视图的Request里还保存着之前的Params内容,但是数据却没有加载上去,我也没有去深究,感觉如果在这里直接把filterContext.Controller中的ViewData直接作为新的ViewResult的ViewData的话,肯定是可以显示提交之前的数据的(因为如果将异常代码包在try...catch...里面是可以在异常后显示之前数据的)。

于是自已新建一个ExceptionFitler:

public class CustomHandleErrorAttribute : FilterAttribute, IExceptionFilter
{
    public void OnException(ExceptionContext filterContext)
    {
        filterContext.Controller.ViewData["Exception"] = filterContext.Exception;
        filterContext.Result = new ViewResult() { ViewName = filterContext.Controller.ControllerContext.RouteData.Values["Action"].ToString(), ViewData = filterContext.Controller.ViewData };
        filterContext.ExceptionHandled = true;
        filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
    }
}

类名起的不咋的,将就着用吧:)

将原来的Action修改如下:

[HttpPost]
[CustomHandleError]
public ActionResult Create(string Name)
{
    throw new Exception("oops...");
}

Create.csthml中加入如下代码:

if (ViewData["Exception"] != null)
{
    var ex = ViewData["Exception"] as Exception;
    @ex.Message
}

F5,果然在提交后又回到了原来视图,而且之前填写的数据都还在。

3月19日完善如下:-----------------------------------------

namespace System.Web.Mvc
{
    public class HandleExceptionAttribute : HandleErrorAttribute, IExceptionFilter
    {
        #region IExceptionFilter Members
 
        public override void OnException(ExceptionContext filterContext)
        {
            if (filterContext == null)
            {
                throw new ArgumentNullException("filterContext");
            }
            if (filterContext.IsChildAction)
            {
                return;
            }
 
            // If custom errors are disabled, we need to let the normal ASP.NET exception handler
            // execute so that the user can see useful debugging information.
            if (filterContext.ExceptionHandled || !filterContext.HttpContext.IsCustomErrorEnabled)
            {
                return;
            }
 
            Exception exception = filterContext.Exception;
 
            // If this is not an HTTP 500 (for example, if somebody throws an HTTP 404 from an action method),
            // ignore it.
            if (new HttpException(null, exception).GetHttpCode() != 500)
            {
                return;
            }
 
            if (!ExceptionType.IsInstanceOfType(exception))
            {
                return;
            }
 
            string actionName = (string)filterContext.RouteData.Values["action"];
            filterContext.Controller.ViewData["Exception"] = exception;
            filterContext.Result = new ViewResult() { ViewName = actionName, ViewData = filterContext.Controller.ViewData };
            filterContext.ExceptionHandled = true;
            filterContext.HttpContext.Response.Clear();
            filterContext.HttpContext.Response.StatusCode = 500;
            filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
        }
 
        #endregion
    }
 
    public static class HandleExceptionHelper
    {
        public static Exception Exception(this HtmlHelper htmlhelper)
        {
            var exception = htmlhelper.ViewContext.Controller.ViewData["Exception"] as Exception;
            return exception;
        }
    }
}

View运用如下:

if (@Html.Exception() != null)
{
    @Html.Exception().Message
}

3月20日添加生成jQuery错误样式:------------------------------------------------

public static class HandleExceptionHelper
{
    public static Exception Exception(this HtmlHelper htmlhelper)
    {
        var exception = htmlhelper.ViewContext.Controller.ViewData["Exception"] as Exception;
        return exception;
    }
 
    public static MvcHtmlString jQueryStyleError(this HtmlHelper htmlhelper)
    {
        var exception = Exception(htmlhelper);
 
        if (exception == null)
        {
            return null;
        }
 
        TagBuilder builder = new TagBuilder("div");
        builder.GenerateId("editordescription");
        builder.AddCssClass("ui-widget ui-state-error ui-corner-all");
        builder.InnerHtml = string.Format(@"<p><span class=""ui-icon ui-icon-alert"" style=""float: left; margin-right: .3em;""></span><strong>{0}: </strong>{1}</p>",
            Resx.Error, string.IsNullOrEmpty(exception.Message) ? Resx.UnknowErrorMessage : exception.Message);
 
        return new MvcHtmlString(builder.ToString(TagRenderMode.Normal));
    }
}

View应用如下:

@Html.jQueryStyleError()

效果如下:

ASP.NET MVC异常处理方案的更多相关文章

  1. ASP.NET MVC异常处理

    ASP.NET MVC异常处理方案 如何保留异常前填写表单的数据 ASP.NET MVC中的统一化自定义异常处理 MVC过滤器详解 MVC过滤器使用案例:统一处理异常顺道精简代码 ASP.NET MV ...

  2. 一个简单的ASP.NET MVC异常处理模块

    一.前言 异常处理是每个系统必不可少的一个重要部分,它可以让我们的程序在发生错误时友好地提示.记录错误信息,更重要的是不破坏正常的数据和影响系统运行.异常处理应该是一个横切点,所谓横切点就是各个部分都 ...

  3. ASP.NET mvc异常处理的方法

    第一种:全局异常处理 1.首先常见保存异常的类(就是将异常信息写入到文件中去) public class LogManager { private string logFilePath = strin ...

  4. ASP.NET MVC Anti-XSS方案

    1:Form提交模式 在使用Form提交时,MVC框架提供了一个默认的机制.如果数据中含有恶意字,则会自动转向出错页面.   2:Ajax+JSON提交模式. MVC框架未提供对于Json数据的Ant ...

  5. asp.net MVC 异常处理

    http://www.cnblogs.com/think8848/archive/2011/03/18/1987849.html http://www.cnblogs.com/snowdream/ar ...

  6. NET MVC异常处理模块

    一个简单的ASP.NET MVC异常处理模块   一.前言 异常处理是每个系统必不可少的一个重要部分,它可以让我们的程序在发生错误时友好地提示.记录错误信息,更重要的是不破坏正常的数据和影响系统运行. ...

  7. ASP.NET MVC 多语言方案

    前言: 好多年没写文章了,工作很忙,天天加班, 每天都相信不用多久,就会升职加薪,当上总经理,出任CEO,迎娶白富美,走上人生巅峰,想想还有点小激动~~~~ 直到后来发生了邮箱事件,我竟然忘了给邮箱密 ...

  8. 七天学会ASP.NET MVC (六)——线程问题、异常处理、自定义URL

    本节又带了一些常用的,却很难理解的问题,本节从文件上传功能的实现引出了线程使用,介绍了线程饥饿的解决方法,异常处理方法,了解RouteTable自定义路径 . 系列文章 七天学会ASP.NET MVC ...

  9. Asp.net Mvc 身份验证、异常处理、权限验证(拦截器)实现代码

    本问主要介绍asp.net的身份验证机制及asp.net MVC拦截器在项目中的运用.现在让我们来模拟一个简单的流程:用户登录>权限验证>异常处理 1.用户登录 验证用户是否登录成功步骤直 ...

随机推荐

  1. c语言gets()函数与它的替代者fgets()函数

    在c语言中读取字符串有多种方法,比如scanf() 配合%s使用,但是这种方法只能获取一个单词,即遇到空格等空字符就会返回.如果要读取一行字符串,比如: I love BIT 这种情况,scanf() ...

  2. JAVA程序员必看的15本书-JAVA自学书籍推荐

    作为Java程序员来说,最痛苦的事情莫过于可以选择的范围太广,可以读的书太多,往往容易无所适从.我想就我自己读过的技术书籍中挑选出来一些,按照学习的先后顺序,推荐给大家,特别是那些想不断提高自己技术水 ...

  3. 隐马尔科夫模型研究 stock 以及 lotto

    说明 本文参考了这里 由于数据是连续的,因此使用了高斯隐马尔科夫模型:gaussianHMM 一.stock代码 import tushare as ts import pandas as pd im ...

  4. shell脚本事例 -- 获取当前日期的前一天日期

    记录一个shell脚本事例,事例中包括shell的一些语法(函数定义.表达式运算.if.case...) #!/bin/sh #获取当前时间 RUN_TIME=`date +%H%M%S` #取当前日 ...

  5. 使用 vi/vim 时,粘贴进新创建文件或空文件的首行内容丢失的解决方法

    只需要进入插入模式后,回车空一行或几行,再粘贴,再把上面的几个空行back回去,就不会丢失首行的内容了.

  6. AlarmManager守护服务和隐藏桌面图标

    1.主要内容 本章记录几段常用代码: 1.如何使用AlarmManager守护服务2.如何判断某服务是否正在运行 2.如何暂时禁用Android的组件 2.使用AlarmManager守护服务 Boo ...

  7. Kosaraju算法、Tarjan算法分析及证明--强连通分量的线性算法

    一.背景介绍 强连通分量是有向图中的一个子图,在该子图中,所有的节点都可以沿着某条路径访问其他节点.强连通性是一种非常重要的等价抽象,因为它满足 自反性:顶点V和它本身是强连通的 对称性:如果顶点V和 ...

  8. 测试开发:Python+Django实现接口测试工具

    Python+Django接口自动化 引言: 最近被几个公司实习生整自闭了,没有基础,想学自动化又不知道怎么去学,没有方向没有头绪,说白了其实就是学习过程中没有成就感,所以学不下去.出于各种花里胡哨的 ...

  9. springboot 前后端分离开发 从零到整(一、环境的搭建)

    第一次写文章,有什么错误地方请大家指正,也请大家见谅. 这次为大家分享我做毕业设计的一个过程,之前没有接触过springboot,一直做的都是Javaweb和前端,做了几个前后端分离的项目.现在听说s ...

  10. SqlHelper DBHelper

    根据自己项目的开发需要,整理了一个SqlHelper类 相比较网上通用的SqlHelper类方法主要有一下几点的不同: 1.因为要操作多个数据库,所以数据库连接字符串没有写死到方法里,作为参数提供出来 ...