捕捉异常:

using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Web;
using System.Web.Http.Controllers;
using System.Web.Mvc;
using Newtonsoft.Json;
using System.Web.Http.Filters;
using Rongzi.BZone.Admin.Models;
using Rongzi.BZone.Common.Util; namespace Rongzi.BZone.Admin.Functions
{
public class ApiExceptionFilterAttribute : System.Web.Http.Filters.ExceptionFilterAttribute
{
public override void OnException(HttpActionExecutedContext context)
{
if (context.Exception is NotImplementedException)
{
context.Response = new HttpResponseMessage(HttpStatusCode.NotImplemented);
}
LogHelper.Error("被系统过滤捕获的异常", context.Exception); StringBuilder sbMsg = new StringBuilder();
sbMsg.AppendFormat("ControllerName={0},ActionName={1},Msg={2},StackTrace={3}",
context.ActionContext.ControllerContext.ControllerDescriptor.ControllerName,
context.ActionContext.ActionDescriptor.ActionName,
context.Exception.Message,
context.Exception.StackTrace);
LogHelper.Exception(sbMsg.ToString()); var customerError = new ResponseContext
{
Head = new ResponseHead
{
Ret = -,
Code = ErrCode.Failure,
Msg = context.Exception.Message.ToString()
},
Content = ""
};
context.Response = context.Request.CreateResponse(HttpStatusCode.BadRequest, customerError);
}
}
}

找不到地址

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using System.Web; namespace Rongzi.BZone.Admin.Functions
{
/// <summary>
/// api返回异常状态时响应处理
/// </summary>
public class ResponseDelegatingHandler : DelegatingHandler
{
async protected override Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request, CancellationToken cancellationToken)
{
HttpResponseMessage response = await base.SendAsync(request, cancellationToken);
if (response.StatusCode == HttpStatusCode.MethodNotAllowed || response.StatusCode == HttpStatusCode.NotFound)
{
var customerError = new ResponseContext
{
Head = new ResponseHead
{
Ret = -,
Code = ErrCode.Failure,
Msg = (response.StatusCode == HttpStatusCode.MethodNotAllowed) ? "请求的资源上不允许请求方法(POST或GET)" : "访问的页面不存在"
},
Content = ""
};
response = request.CreateResponse(HttpStatusCode.BadRequest, customerError);
}
return response;
}
}
}

MVC 应用程序级别捕捉异常的更多相关文章

  1. .NET [MVC] 利用特性捕捉异常

    声明:本代码只适用于.NET MVC. 先创建一个类继承ActionFilterAttribute这个抽象类以及实现IExceptionFilter接口,并实现它的方法OnException. 代码如 ...

  2. .NET Core[MVC] 利用特性捕捉异常

    声明:本方式适用于MVC.本代码只适用于.NET Core MVC. 先创建一个类继承ExceptionFilterAttribute这个抽象类,并override它的方法OnException. 代 ...

  3. C#程序如何捕捉未try/catch的异常——不弹“XXX已停止工作”报错框

    诚意满满直接上代码: static void Main(string[] args) { //Main函数中增加此句 AppDomain.CurrentDomain.UnhandledExceptio ...

  4. [渣译文] 使用 MVC 5 的 EF6 Code First 入门 系列:为ASP.NET MVC应用程序创建更复杂的数据模型

    这是微软官方教程Getting Started with Entity Framework 6 Code First using MVC 5 系列的翻译,这里是第六篇:为ASP.NET MVC应用程序 ...

  5. [渣译文] 使用 MVC 5 的 EF6 Code First 入门 系列:为ASP.NET MVC应用程序处理并发

    这是微软官方教程Getting Started with Entity Framework 6 Code First using MVC 5 系列的翻译,这里是第十篇:为ASP.NET MVC应用程序 ...

  6. ASP.NET MVC应用程序处理并发

    为ASP.NET MVC应用程序处理并发 2014-05-14 08:37 by Bce, 694 阅读, 2 评论, 收藏, 编辑 这是微软官方教程Getting Started with Enti ...

  7. 为ASP.NET MVC应用程序创建更复杂的数据模型

    为ASP.NET MVC应用程序创建更复杂的数据模型 2014-05-07 18:27 by Bce, 282 阅读, 1 评论, 收藏, 编辑 这是微软官方教程Getting Started wit ...

  8. MVC应用程序请求密码的功能1

    MVC应用程序请求密码的功能(一) 经过一系列的练习,实现了会员注册<MVC会员注册>http://www.cnblogs.com/insus/p/3439599.html,登录<M ...

  9. 使用Metrics.NET 构建 ASP.NET MVC 应用程序的性能指标

    通常我们需要监测ASP.NET MVC 或 Web API 的应用程序的性能时,通常采用的是自定义性能计数器,性能计数器会引发无休止的运维问题(损坏的计数器.权限问题等).这篇文章向你介绍一个新的替代 ...

随机推荐

  1. Union大小

    在一个union类型结构中,所有的成员公用同样的存储空间,其占用的大小为其成员中需要空间最大者: union本身只保留一块地址空间,因为只有一个成员真正存储于该地址, 但这块地址也要满足内存对齐原则. ...

  2. 【转】TCP协议中的三次握手和四次挥手(图解)

    http://blog.csdn.net/whuslei/article/details/6667471

  3. ZOJ 2975 思维

    题意 给出一个矩形 问在其中存在多少子矩形 其四个角上的字母是一样的 一开始暴力写了一发 先枚举行数 再枚举两个列数 再向下枚举行数 判断能否 没有意外的超时了 后来想了想 当我们已经确定两个列数的时 ...

  4. PHP里用户密码的回复和管理

    1). In /etc/my.ini, add skip-grant-tables 2). mysql -u root -p            (no password required) mys ...

  5. UIbutton 和UIview 切单角

    UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];    btn.backgroundColor = [UIColor cya ...

  6. GDC2016【彩虹六号:围攻 】使丰富的“突破”成为可能的破坏系统

      [彩虹六号:围攻 ]使得丰富的“突破”成为可能的破坏系统 深奥的战术游戏的背景下,最新的程序化破坏技术[REALBLAST]!     进行演讲的是Ubisoft Montreal的Julien ...

  7. C++类型转化分析(1)

    仔细想想地位卑贱的类型转换功能(cast),其在程序设计中的地位就象goto语句一样令人鄙视.但是它还不是无法令人忍受,因为当在某些紧要的关头,类型转换还是必需的,这时它是一个必需品. 不过C风格的类 ...

  8. FloodLight使用感受

    一个使用java语言编写的基于Openflow协议的SDN控制器. 基本架构同webserver一样,有一个维护交换机连接信息的底层模块,当有交换机同控制器连接时,floodlight会将此连接保存到 ...

  9. MySQL线程独享[转]

    一.前言在 MySQL 中,线程独享内存主要用于各客户端连接线程存储各种操作的独享数据,如线程栈信息,分组排序操作,数据读写缓冲,结果集暂存等等,而且大多数可以通过相关参数来控制内存的使用量。 二.线 ...

  10. TCPdump抓包命令详解--摘

    http://blog.csdn.net/s_k_yliu/article/details/6665673/ http://starsliao.blog.163.com/blog/static/890 ...