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的!的更多相关文章

  1. ASP.NET MVC & WebApi 中实现Cors来让Ajax可以跨域访问 (转载)

    什么是Cors? CORS是一个W3C标准,全称是"跨域资源共享"(Cross-origin resource sharing).它允许浏览器向跨源服务器,发出XMLHttpReq ...

  2. mvc中使用remote属性来做ajax验证

    mvc中使用remote属性来做ajax验证比較easy : [Remote("Action", "Controller", AdditionalFields ...

  3. 统一的mvc异常处理

    mvc异常处理 using System; using System.Configuration; using System.Web.Mvc; using Infrastructure.Excepti ...

  4. ajax 文件上传,ajax

    ajax 文件上传,ajax 啥也不说了,直接上代码! <input type="file" id="file" name="myfile&qu ...

  5. ASP.NET MVC异常处理

    ASP.NET MVC异常处理方案 如何保留异常前填写表单的数据 ASP.NET MVC中的统一化自定义异常处理 MVC过滤器详解 MVC过滤器使用案例:统一处理异常顺道精简代码 ASP.NET MV ...

  6. 浅谈AJAX的基本原理和原生AJAX的基础用法

    一.什么是AJAX? AJAX,即"Asynchronous Javascript And XML",翻译为异步的JavaScript和XML,是一种创建交互式网页应用的网页开发技 ...

  7. JavaScript封装Ajax(类JQuery中$.ajax()方法)

    ajax.js (function(exports, document, undefined){ "use strict"; function Ajax(){ if(!(this ...

  8. 详细解读Jquery各Ajax函数:$.get(),$.post(),$.ajax(),$.getJSON()

    一,$.get(url,[data],[callback]) 说明:url为请求地址,data为请求数据的列表(是可选的,也可以将要传的参数写在url里面),callback为请求成功后的回调函数,该 ...

  9. 掌握 Ajax,第 1 部分: Ajax 入门简介

    转:http://www.ibm.com/developerworks/cn/xml/wa-ajaxintro1.html 掌握 Ajax,第 1 部分: Ajax 入门简介 理解 Ajax 及其工作 ...

  10. 详细解读Jquery各Ajax函数:$.get(),$.post(),$.ajax(),$.getJSON() —(转)

    一,$.get(url,[data],[callback]) 说明:url为请求地址,data为请求数据的列表(是可选的,也可以将要传的参数写在url里面),callback为请求成功后的回调函数,该 ...

随机推荐

  1. 构建基于Jenkins + Github的持续集成环境

    搭建持续集成首先要了解什么是持续集成,带着明确的目标去搭建持续集成环境才能让我们少走很多弯路.持续集成(Continuous integration)简称CI,是一种软件开发的实践,可以让团队在持续集 ...

  2. Android broadcast

    发送广播而且接受.发送两个广播 Intent intent = new Intent(); intent.setAction("com.wxq.CUSTOM_INTENT"); s ...

  3. 判断Webbrowser是否加载完成

    var Form1: TForm1; glpDisp: IDispatch = nil; implementation {$R *.dfm} procedure TForm1.EmbeddedWB1D ...

  4. 14.2.5.5 Change Buffer

    14.2.5.5 Change Buffer change buffer是一个指定的数据结构 用于caches 数据到secondary index pages 当影响的pages 不是在buffer ...

  5. 单例模式(Singleton)Holder

    public class Singleton { /** * 类级的内部类,也就是静态的成员式内部类,该内部类的实例与外部类的实例 * 没有绑定关系,而且只有被调用到才会装载,从而实现了延迟加载 */ ...

  6. POJ 3040 Allowance 贪心

    这题目的贪心思路还是有一点细节问题的. 还没有证明,据说是因为题目给的条件是每个价格是比它小的价格的倍数才能这么贪心的. 思路如下: 假设要给奶牛的钱为C 1)从大面值到小面值一次拿钱,能拿多少拿多少 ...

  7. CS0433: 类型“BasePage”同一时候存在于“c:\Windows\Microsoft.NETxxxxxxxxxxxxxxxx

    网上常见的我就不说了. 假设其他地址的方法解决不了你的问题,那么请往下看. 该类是否存放于 App_Code 下,假设是把该类从App_Code中拉出来,然后再次执行试试.

  8. Android开发四大组件概述

    这个文章主要是讲Android开发的四大组件,本文主要分为 一.Activity具体解释 二.Service具体解释 三.Broadcast Receiver具体解释 四.Content Provid ...

  9. 基于DSP的疲劳驾驶检测系统的研究

    原地址:http://www.chinaaet.com/article/index.aspx?id=114534 关键词:疲劳检测DSP亮瞳效应PERCLOS 摘  要: 针对汽车驾驶员疲劳驾驶检测的 ...

  10. 日交易41.9亿,B2B的魅力为何不输于B2C、C2C?

        在最近两年的电子商务版图中,B2C和C2C可谓大放异彩,相比之下,B2B却显得颇为“低调”,当然,低调并不代表没有影响力,只不过,相比B2C和C2C面向数亿网民而言,B2B只针对企业和商家服务 ...