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为请求成功后的回调函数,该 ...
随机推荐
- Android 界面滑动实现---Scroller类 从源码和开发文档中学习(让你的布局动起来)
在android学习中,动作交互是软件中重要的一部分,其中的Scroller就是提供了拖动效果的类,在网上,比如说一些Launcher实现滑屏都可以通过这个类去实现.. 例子相关博文:Androi ...
- 开源解析器--ANTLR
序言 有的时候,我还真是怀疑过上本科时候学的那些原理课究竟是不是在浪费时间.比方学完操作系统原理之后我们并不能自己动手实现一个操作系统:学完数据库原理我们也不能弄出个像样的DBMS出来:相同,学完 ...
- ASP.NET - cookie
下面是写cookie HttpCookie cookie = new HttpCookie("Info");//定义cookie对象以及名为Info的项 DateTime dt ...
- webdynpro 调用应用程序做跳转
1.是调用指的是调用生成应用程序,非webdynpro组件程序, 如下: 1)调用页面,并传值 METHOD get_zgmtpage . DATA:lw_application_name TYPE ...
- jquery用div模拟一个下拉列表框
原文 jquery用div模拟一个下拉列表框 今天分享一个用我自己用jquery写的,用div模拟下拉列表select,这个效果网上有很多,但是写一个有自己思路的代码效果,更有成就感,先看截图: 自我 ...
- linux下编译原理分析
linux下编译hello.c 程序,使用gcc hello.c,然后./a.out就能够执行:在这个简单的命令后面隐藏了很多复杂的过程,这个过程包含了以下的步骤: ================= ...
- 一个解析RTSP 的URL函数
写了一个解析URL的函数,可以提取URL中的IP 和 port. 如:url = "rtsp://192.168.1.43:2554/realmp3.mp3"; url = &qu ...
- Delphi中复制带有String的记录结构时不能使用Move之类的内存操作函数
请看下面的代码: program TestRecord; {$APPTYPE CONSOLE} uses SysUtils, Math; type TRecordA = record Na ...
- Button UI Kit CSS3美丽Buttonbutton
<!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8& ...
- 【android】禁止Edittext弹出软键盘而且使光标正常显示
/** * 禁止Edittext弹出软件盘,光标依旧正常显示. */ public void disableShowSoftInput() { if (android.os.Build.VERSION ...