Mvc 异常处理 ajax的 和 不是ajax的!
using ImageUpload.Auth;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc; namespace Web
{
[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 = AjaxError(filterContext.Exception.Message, filterContext);
filterContext.ExceptionHandled = true;
}
else
{
Er.G(errorMsg);//把错误存储起来
filterContext.Result = new RedirectResult("/Home/ErrorMsg/");
filterContext.ExceptionHandled = true;
}
}
protected JsonResult AjaxError(string message, ExceptionContext filterContext)
{
//如果消息是null或空,则充满了通用的消息
if (String.IsNullOrEmpty(message))
message = "发生了一些错误在处理您的请求。请刷新页面再试一次."; //响应状态代码设置为500
filterContext.HttpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError; //需要 IIS7.0
filterContext.HttpContext.Response.TrySkipIisCustomErrors = true; return new JsonResult
{
//可以扩展更多的属性
Data = new AjaxExceptionModel() { ErrorMessage = message },
ContentEncoding = System.Text.Encoding.UTF8,
JsonRequestBehavior = JsonRequestBehavior.DenyGet
};
}
}
public class AjaxExceptionModel
{
public string ErrorMessage { get; set; }
}
}
Er 类用来存储错误信息 然后前台调用
public static class Er
{
public static string Msg { get; set; }
/// <summary>
/// 将错误信息存起来
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static string G(string s)
{
Msg = s;
return Msg;
}
/// <summary>
/// 读取错误信息
/// </summary>
/// <returns></returns>
public static string P()
{
return Msg;
} }
创建一个ErrorMsg控制器 和 视图
public ActionResult ErrorMsg()
{
string ss = Er.P();
ViewBag.msg = Er.P();
return View();
}
视图
@{
ViewBag.Title = "";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div id="div" style="display:none">
<h2>@ViewBag.msg </h2>
<h3><a href="/Home/Index">返回首页</a></h3>
<p>给您造成影响,再次向您表示道歉!</p>
</div>
@section scripts
{
<script>
@if (ViewBag.msg!=null)
{
<text>
swal(
{
title: "错误?",
text: "@ViewBag.msg!",
type: "error",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "跳到首页",
cancelButtonText: "不",
closeOnConfirm: false,
closeOnCancel: false,
allowOutsideClick:true
},
function (isConfirm) {
if (isConfirm) {
window.location.href = "/Home/Index";
swal("请等待!", "正在跳转。。。", "success");
} else {
swal(
{
title: "取消",
text: "您取消了跳转.",
type: "warning",
confirmButtonText:"确定",
timer:
});
$("#div").show();
}
});
</text>
}
</script>
}
视图用到了sweetalert 弹窗插件
下面是ajax的全局错误处理
创建一个 MyJs 的js文件
$(document).ajaxError(function (event, xhr, options, exc) {
if (xhr.responseText != "") {
var jsonValue = jQuery.parseJSON(xhr.responseText);
console.log(jsonValue.ErrorMessage);
swal(
{
title: "错误",
text: jsonValue.ErrorMessage,
type: "warning",
confirmButtonText: "确定",
});
}
})
Mvc 异常处理 ajax的 和 不是ajax的!的更多相关文章
- ASP.NET MVC & WebApi 中实现Cors来让Ajax可以跨域访问 (转载)
什么是Cors? CORS是一个W3C标准,全称是"跨域资源共享"(Cross-origin resource sharing).它允许浏览器向跨源服务器,发出XMLHttpReq ...
- mvc中使用remote属性来做ajax验证
mvc中使用remote属性来做ajax验证比較easy : [Remote("Action", "Controller", AdditionalFields ...
- 统一的mvc异常处理
mvc异常处理 using System; using System.Configuration; using System.Web.Mvc; using Infrastructure.Excepti ...
- ajax 文件上传,ajax
ajax 文件上传,ajax 啥也不说了,直接上代码! <input type="file" id="file" name="myfile&qu ...
- ASP.NET MVC异常处理
ASP.NET MVC异常处理方案 如何保留异常前填写表单的数据 ASP.NET MVC中的统一化自定义异常处理 MVC过滤器详解 MVC过滤器使用案例:统一处理异常顺道精简代码 ASP.NET MV ...
- 浅谈AJAX的基本原理和原生AJAX的基础用法
一.什么是AJAX? AJAX,即"Asynchronous Javascript And XML",翻译为异步的JavaScript和XML,是一种创建交互式网页应用的网页开发技 ...
- JavaScript封装Ajax(类JQuery中$.ajax()方法)
ajax.js (function(exports, document, undefined){ "use strict"; function Ajax(){ if(!(this ...
- 详细解读Jquery各Ajax函数:$.get(),$.post(),$.ajax(),$.getJSON()
一,$.get(url,[data],[callback]) 说明:url为请求地址,data为请求数据的列表(是可选的,也可以将要传的参数写在url里面),callback为请求成功后的回调函数,该 ...
- 掌握 Ajax,第 1 部分: Ajax 入门简介
转:http://www.ibm.com/developerworks/cn/xml/wa-ajaxintro1.html 掌握 Ajax,第 1 部分: Ajax 入门简介 理解 Ajax 及其工作 ...
- 详细解读Jquery各Ajax函数:$.get(),$.post(),$.ajax(),$.getJSON() —(转)
一,$.get(url,[data],[callback]) 说明:url为请求地址,data为请求数据的列表(是可选的,也可以将要传的参数写在url里面),callback为请求成功后的回调函数,该 ...
随机推荐
- 美国企业人事喜欢考的3道.Net经典笔试题
1..求以下表达式的值,写出您想到的一种或几种实现方法: 1-2+3-4+……+m 答: int Num = this.TextBox1.Text.ToString() ; int Sum = 0 ; ...
- DRP分销系统总结
上个月看完的分销系统的视频,用了漫长的时间看这个项目视频,能安慰自己的是不光是看视频了,还做了很多自己想做的事情,比如驾照拿下来了,比如参加了一些考试,比如讲了一些课程等等.把这个系统的总结总算是补上 ...
- catalan 数——卡特兰数(转)
Catalan数——卡特兰数 今天阿里淘宝笔试中碰到两道组合数学题,感觉非常亲切,但是笔试中失踪推导不出来后来查了下,原来是Catalan数.悲剧啊,现在整理一下 一.Catalan数的定义令h(1) ...
- io 测试
//其中能够实现编码的只有OutputStreamWriter和对应inputStreamReader package net; import java.io.BufferedWriter; impo ...
- Jsoup API解析HTML中input标签
Jsoup官网地址:http://jsoup.org/ 1. 解析单个input元素 String html = "<p><input align=\"t ...
- DataGridView ——管理员对用户的那点操作
记得第一次做机房收费系统的时候,就在加入删除用户这出现了点小问题,由于一直都是一个容不得一点瑕疵的人.所以对查询用户的时候查询一次就会多一些空行我非常是不能容忍.看似非常小的问题,我却花了非常长的时间 ...
- hdoj 2066 一个人的旅行 【多源多汇最短路】
题目:hdoj 2066 一个人的旅行 方法:缩点 + 最短路 分析:看了大神的一篇博客,讲冗余压缩的,然后就想找一个多源最短路练练手. 这个题目就是典型的多源多汇最短路 方法:把全部的源点压缩成一个 ...
- DFA最小化 -- Hopcroft算法 Python实现
wiki 伪代码看上去一直以为怪.发现葡萄牙语和俄罗斯语那里的 if 推断都还缺少一个条件. 国内的资料比較少.这几份学习资料不错.比我稀里糊涂的思路要好,分享下: http://www.liafa. ...
- TPL异步并行编程之回调
Task是基于ThreadPool线程池基础上的高度异步多线程编程,如果有一天我希望一个需要长时间运行的Task,在被某些异常终止后还能回调一些代码就可以知道Task终止的原因了吧 是的,且看代码 p ...
- Effective C++:规定12:不要忘了复制的对象时,它的每一个组成部分
(一个) 继承制度的声明: class Date {...}; class Customer { public: ... private: string name; Date lastTransact ...