报错拦截过滤到error页面

    [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = true, AllowMultiple = true)]
public class ExceptionAttribute : FilterAttribute, IExceptionFilter
{
/*报错回到报错页Error*/
/*
*CreateTime:2013‎年‎3‎月‎18‎日
*/
public void OnException(ExceptionContext filterContext)
{
//报错回到报错页
string message = string.Format("消息类型:{0}<br>消息内容:{1}<br>引发异常的方法:{2}<br>引发异常源:{3}"
, filterContext.Exception.GetType().Name
, filterContext.Exception.Message
, filterContext.Exception.TargetSite
, filterContext.Exception.Source + filterContext.Exception.StackTrace
);
filterContext.Controller.ViewData["ErrorMessage"] = message;//filterContext.Exception.Message + " 亲!您犯错了哦!";//得到报错的内容
filterContext.Result = new ViewResult()//new一个url为Error视图
{
ViewName = "Error",/*在Shard文件夹下的Error.cshtml*/
ViewData = filterContext.Controller.ViewData//view视图的属性中的viewdata被赋值
}; filterContext.ExceptionHandled = true;
} }

修改上面功能,增加ajax错误判断

/*报错回到报错页Error*/
/*
*CreateTime:2013‎年‎3‎月‎18‎日
*/
public void OnException(ExceptionContext filterContext)
{
//报错回到报错页
string message = string.Format("消息类型:{0}<br>消息内容:{1}<br>引发异常的方法:{2}<br>引发异常源:{3}"
, filterContext.Exception.GetType().Name
, filterContext.Exception.Message
, filterContext.Exception.TargetSite
, filterContext.Exception.Source + filterContext.Exception.StackTrace
);
filterContext.Controller.ViewData["ErrorMessage"] = message;//filterContext.Exception.Message + " 亲!您犯错了哦!";//得到报错的内容 if (filterContext.HttpContext.Request.IsAjaxRequest())
{
filterContext.Result = new ContentResult()
{
Content = "Error",
ContentEncoding = Encoding.UTF8
};
}
else {
filterContext.Result = new ViewResult()//new一个url为Error视图
{
ViewName = "Error",/*在Shard文件夹下*/
ViewData = filterContext.Controller.ViewData//view视图的属性中的viewdata被赋值
};
} filterContext.ExceptionHandled = true;
}

MVC的Filters(拦截过滤)的Error页面,支持Ajax报错的更多相关文章

  1. Centos系统创建用户oracle后,用该用户登陆系统,页面加载报错GConf error

    Linux 的 GConf error 解决办法 问题: Centos系统创建用户oracle后,用该用户登陆系统,页面加载报错,导致重新进入Centos系统后出现: GConf error:Fail ...

  2. Eclipse 新建.jsp页面后,页面头部标签报错的解决方法

    Eclipse 新建.jsp页面后,页面头部标签报错的解决方法 1.报错地方: 2.解决方法: .jsp页面右键==>BUild Path ==>Configure Build Path. ...

  3. saiku中过滤窗口优化及隐藏异常报错

    问题一:当取消自动查询后,点击该维度应弹出过滤条件窗口,实际无反应,只有执行一次查询后再点击该维度,才能弹出过滤条件窗口 解决办法:打开WorkspaceDropZone.js文件,找到selecti ...

  4. Ajax 报错 500 (Internal Server Error)

    ==========error======={"readyState":4,"responseText":"<html><head& ...

  5. [eclipse]“Syntax error, insert "}" to complete”报错的解决方案

    背景:本人在网上学习java时,看到一段样例代码比较好,因此复制粘贴到eclipse中看看编译结果.结果eclipse报"Syntax error, insert "}" ...

  6. 创建一个MVC解决方案,添加一个控制器后,运行程序报错:”/"未找到服务器

    1.创建一个MVC项目,如图

  7. Error- Overloaded method value createDirectStream in error Spark Streaming打包报错

    直接上代码 StreamingExamples.setStreamingLogLevels() val Array(brokers, topics) = args // Create context ...

  8. Java 过滤器Filter,Java Filter 不拦截某些请求 Java 过滤器支持Ajax请求

    ================================ ©Copyright 蕃薯耀 2020-01-10 https://www.cnblogs.com/fanshuyao/ 一.Java ...

  9. 解决 yum安装时报错 Error: Protected multilib versions: 报错

    系统中缺少一个lib库 libz.so.1文件,使用yum安装会自动找到相关的rpm包,如下命令 # yum -y install libz.so.1 Resolving Dependencies-- ...

随机推荐

  1. The Ninth Hunan Collegiate Programming Contest (2013) Problem G

    Problem G Good Teacher I want to be a good teacher, so at least I need to remember all the student n ...

  2. DP三角形

    Hrbust1038  http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=1038 // ...

  3. 初学cocos2dx-3.x之使用Scale9Sprite时的配置问题

    今天学习的时候遇到了Scale9Sprite————一张可拉伸的精灵,只要给它设置一下大小,它就会自动拉伸. Scale9Sprite* nineGirl = Scale9Sprite::create ...

  4. 全局变量&局部变量

    一.局部变量: 定义在函数内部的变量以及函数的形参成为局部变量 作用于:从定义那一行开始知道与其所在的代码块结束 生命周期:从程序运行到定义那一行开始分配存储空间到程序离开该变量所在的作用域 特点: ...

  5. 是否连接VPN

    //需要导入ifadds头文件 //是否连接VPN - (BOOL)isVPNConnected{     struct ifaddrs *interfaces = NULL;     struct ...

  6. C#实现文件下载的几种方法

    //WriteFile实现下载 protected void Button2_Click(object sender, EventArgs e) { /* using System.IO; */ st ...

  7. Linux平台下Lotus Domino服务器部署案例

    Linux平台下Lotus Domino服务器部署案例 几年前我写了篇<RHAS2.1下安装中文LotusDominoR6.5图解>这篇文档被多个大型网站转载,曾帮助过很多公司系统管理员部 ...

  8. CSS现代字体栈

    CSS字体栈是一系列的字体,它包含了能在不同操作系统和平台上战士的字体,以尽可能的使排版保持一致性.浏览器会在font-family规定的所有字体中从前往后一次查找,如果找不到当前字体就查找下一个字体 ...

  9. html5 搖一搖

    <script> // 首先在页面上要监听运动传感事件 function init(){ if (window.DeviceMotionEvent) { // 移动浏览器支持运动传感事件 ...

  10. 开源项目:网页实时通信WebRTC

    参考资料 [博客系列] Android WebRTC 音视频开发总结 [环境编译] WebRTC入门指南 各平台WebRTC源码网盘下载 Ubuntu14.04编译WebRTC For Android ...