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 中加上标签就可以了
时间: 2024-11-05 20:28:24