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为请求成功后的回调函数,该 ...
随机推荐
- 十天学习PHP之第三天
1)按右边的结构:查看改动表结构 2)按右边的浏览:查看表中的数据 3)按右边的SQL:执行SQL语句 4)按右边的插入:插入一行记录 5)按右边的清空:删除表中全部记录 6)按右边的删除: ...
- android中保存Bitmap图片到指定文件夹中的方法
/** 保存方法 */ public void saveBitmap() { Log.e(TAG, "保存图片"); File f = new File("/s ...
- ASP.NET - 多级分类
表结构: 表数据: 最终效果: 前端代码: <%@ Page Language="C#" AutoEventWireup="true" CodeBehin ...
- Thinkphp入门 二 —空操作、空模块、模块分组、前置操作、后置操作、跨模块调用(46)
原文:Thinkphp入门 二 -空操作.空模块.模块分组.前置操作.后置操作.跨模块调用(46) [空操作处理] 看下列图: 实际情况:我们的User控制器没有hello()这个方法 一个对象去访问 ...
- jd.py
#!/usr/bin/env python #coding:utf-8 import urllib2,re,sys,os,types #from bs4 import BeautifulSoup re ...
- Vmdk文件如何使用,vmdk导入虚拟机VMware8.0教程
一.打开虚拟机VMware Workstation8.0,点击新建虚拟机. 二.进入虚拟机向导,选择自定义. 三.这里保持默认即可. 四.这里选择“我以后安装操作系统”. 五.这里选择Windows ...
- ios23-文件上传
1.上传图片 3. // // ios23_uploadViewController.h // ios23-upload // // Created by on 13-6-17. // Co ...
- System.getProperty("catalina.home")
System.getProperty("catalina.base"),服务器配置目录
- Win32环境下的程序崩溃异常定位
1 案例描述 作为Windows程序员,平时最担心见到的事情可能就是程序发生了崩溃(异常),这时Windows会提示该程序执行了非法操作,即将关闭.请与您的供应商联系.呵呵,这句微软的“名 ...
- JSP内置对象Session
创建和获取客户的会话 setAttribute()与getAttribute() session.setAttribute(String name , Object obj) 如session.set ...