标签:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace MvcApplication1.Controllers { [AttributeUsage(AttributeTargets.Class|AttributeTargets.Method,AllowMultiple=true,Inherited=true)] public class HandleAjaxErrorAttribute:FilterAttribute,IExceptionFilter { public void OnException(ExceptionContext filterContext) { var errorMsg = filterContext.Exception.Message; if (filterContext.HttpContext.Request.IsAjaxRequest()) { filterContext.Result = new JsonResult() { Data = errorMsg, JsonRequestBehavior = JsonRequestBehavior.AllowGet }; filterContext.ExceptionHandled = true; } else { filterContext.HttpContext.Response.Redirect("Http://www.baidu.com"); filterContext.ExceptionHandled = true; } } } }
先新建上面这个类 然后 在Controllers 中加上标签就可以了
标签:
原文地址:http://www.cnblogs.com/crazyair/p/4534320.html