Mvc异常处理器
using System;
using System.Text;
using EMS.Domains.Core;
using System.Web.Mvc;
using Json.Net;
using Util.Webs;
using Json = Util.Json; namespace EMS.MvcUI.Base
{
public class FilterExceptionAttribute : FilterAttribute, IExceptionFilter
{
public void OnException(ExceptionContext filterContext)
{
var isAjax = filterContext.HttpContext.Request.IsAjaxRequest();
if (isAjax)
{
if (filterContext.Exception is ShowException)
{
filterContext.Result = new ContentResult
{
Content =
JsonConvert.SerializeObject(
new {Code = StateCode.Fail, Message = filterContext.Exception.Message, Data = ""})
};
}
else
{
filterContext.Result = new ContentResult
{
Content =
JsonConvert.SerializeObject(new {Code = StateCode.Fail, Message = "服务器异常,请联系管理员", Data = ""})
};
}
}
if (!(filterContext.Exception is ShowException))
{
WriteLog(filterContext);
}
filterContext.ExceptionHandled = true;
} private Exception BuildErrorMessage(Exception ex)
{
while (ex.InnerException != null)
{
ex = ex.InnerException;
}
return ex;
} private void WriteLog(ExceptionContext filterContext)
{
var httpContext = filterContext.RequestContext.HttpContext.Request;
var ex = BuildErrorMessage(filterContext.Exception);
// 在出现未处理的错误时运行的代码
StringBuilder _builder = new StringBuilder();
_builder.Append("\r\n------------- 异常信息 ---------------------------------------------------------------");
_builder.Append("\r\n发生时间:" + DateTime.Now.ToString());
_builder.Append("\r\n发生异常页:" + httpContext.Url.ToString());
_builder.Append("\r\n异常信息:" + ex.Message);
_builder.Append("\r\n错误源:" + ex.Source);
_builder.Append("\r\n堆栈信息:" + ex.StackTrace);
_builder.Append("\r\n-----------------------------------------------------------------------------------------\r\n");
//日志物理路径 DateTime date = DateTime.Now;
string path = httpContext.MapPath("~/Log/");
string month = date.ToString("yyyy-MM");
if (!System.IO.Directory.Exists(path + month))
System.IO.Directory.CreateDirectory(path + month);
string currentDate = date.ToString("yyyy-MM-dd");
string savePath = path + month + "/" + currentDate + ".log";
System.IO.File.AppendAllText(savePath, _builder.ToString(), System.Text.Encoding.Default);
//filterContext.RequestContext.HttpContext.Server.ClearError();
//filterContext.ExceptionHandled = true; //filterContext.RequestContext.HttpContext.Response.Redirect("/Home/Error");
} }
}
Mvc异常处理器的更多相关文章
- 012医疗项目-模块一:统一异常处理器的设计思路及其实现(涉及到了Springmvc的异常处理流程)
我们上一篇文章是建立了一个自定义的异常类,来代替了原始的Exception类.在Serice层抛出异常,然后要在Action层捕获这个异常,这样的话在每个Action中都要有try{}catch{}代 ...
- spring mvc 异常统一处理方式
springMVC提供的异常处理主要有两种方式: 一种是直接实现自己的HandlerExceptionResolver: 另一种是使用注解的方式实现一个专门用于处理异常的Controller——Exc ...
- 关于SpringMVC的全局异常处理器
近几天又温习了一下SpringMVC的运行机制以及原理 我理解的springmvc,是设计模式MVC中C层,也就是Controller(控制)层,常用的注解有@Controller.@RequestM ...
- SpringMVC框架笔记02_参数绑定返回值文件上传异常处理器JSON数据交互_拦截器
目录 第1章 高级参数的绑定 1.1 参数的分类 1.2 数组类型的参数的绑定 1.3 集合类型的参数的绑定 第2章 @RequestMapping的用法 2.1 URL路径映射 2.2 请求方法限定 ...
- 基于SpringMVC的全局异常处理器介绍(转)
近几天又温习了一下SpringMVC的运行机制以及原理 我理解的springmvc,是设计模式MVC中C层,也就是Controller(控制)层,常用的注解有@Controller.@RequestM ...
- SpringMVC实现全局异常处理器 (转)
出处: SpringMVC实现全局异常处理器 我们知道,系统中异常包括:编译时异常和运行时异常RuntimeException,前者通过捕获异常从而获取异常信息,后者主要通过规范代码开发.测试通过手 ...
- 【SpringMVC】文件上传与下载、拦截器、异常处理器
文件下载 使用ResponseEntity实现下载文件的功能 index.html <!DOCTYPE html> <html lang="en" xmlns:t ...
- spring设置全局异常处理器
1.spring设置全局异常,它的原理是向上捕获 spring.xml配置 <!--自定义全局异常处理器--> <bean id="globalExceptionResol ...
- Java 线程异常处理器
Thread.UncaughtExceptionHandler 是Thread类的一个静态内部接口,该接口只有一个方法: void uncaughtException(Thread t, Throwa ...
随机推荐
- python 案例之老王开枪
- @Autowired的作用,自动装配,省去写get/set方法
这个注解就是spring可以自动帮你把bean里面引用的对象的setter/getter方法省略,它会自动帮你set/get. <bean id="userDao" clas ...
- bzoj 4515: 游戏 树链剖分+线段树
题目大意: http://www.lydsy.com/JudgeOnline/problem.php?id=4515 题解: 先让我%一发lych大佬点我去看dalao的题解 讲的很详细. 这里纠正一 ...
- bzoj 4319 Suffix reconstruction —— 贪心构造
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4319 思维还是不行...这样的构造都没思路... 首先,我们可以按 rank 的顺序从小到大 ...
- cocos2dx unzip、createDir
转自:http://www.cnblogs.com/xioapingguo/p/4037323.html static unsigned long _maxUnzipBufSize = 0x50000 ...
- 在VS中对WCF服务进行更新,但是配置文件没有更新解决办法
取消下面的勾选框
- JavaScript高级程序设计学习笔记第二十章--JSON
1.JSON:JavaScript Object Notation, JavaScript 对象表示法. 2.最重要的是要理解它是一种数据格式,不是一种编程语言.虽然具有相同的语法形式,但 JSON ...
- [转]对 td 使用 overflow:hidden; 无效的几点错误认识
转载:http://www.cftea.com/c/2010/12/UVBUCD0J888L2XPQ.asp 一.是 td 的原因. 其实这关 td 什么事呢?div 也是一样的,看示例: <d ...
- linux命令配置IP详解
在Linux系统中,TCP/IP网络是通过若干个文本文件进行配置的,有时需要编辑这些文件来完成联网工作. vi /etc/sysconfig/network-scripts/ifcfg-eth0 :进 ...
- 连接带密码的access数据库
在网上找了很多都不靠谱,稀里哗啦的弄一堆连接字符串,很不优雅. 这个方法很简单: 1.在“连接”这页中,下方有“输入登录数据库的信息”用户名:admin,并在下面选择“空白密码” 2.在“所有”这页的 ...