捕捉异常:

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. 当一回Android Studio 2.0的小白鼠

    上个星期就放出了Android studio出2.0的消息,看了一下what's new 简直抓到了那个蛋疼的编译速度痛点.在网上稍微搜索了一下后发现基本都是介绍视频.一番挣扎后(因为被这IDE坑过几 ...

  2. 《GK101任意波发生器》升级固件发布(版本:1.0.2build198)

    一.固件说明: 硬件版本:0,logic.3 固件版本:1.0.2.build198 编译日期:2014-09-17 ====================================== 二. ...

  3. html5_d登陆界面_注册界面

    <!DOCTYPE html><html><head><script type="text/javascript">function ...

  4. Go语言学习资源

    Go语言学习资源 下载:http://www.golangtc.com/downloadhttp://www.golangtc.com/download/liteide 安装及开发工具http://j ...

  5. 【转】各版本IIS下ASP.net请求处理过程区别

    原文地址:http://www.cnblogs.com/fsjohnhuang/articles/2332074.html ASP.NET是一个非常强大的构建Web应用的平台,它提供了极大的灵活性和能 ...

  6. 自定义view实现侧滑菜单

    自定义View public class SlidingMenu extends HorizontalScrollView { private int mScreenWidth; private in ...

  7. Bootstrap页面布局7 - Bootstrap响应式布局的实用类

    在bootstrap-responsive.css这个CSS样式表中已经为我们设定好了几个实用的类: .visible-phone: 在智能手机设备上显示这个元素,在其他设备上隐藏该元素 .visib ...

  8. Alternative Representations for 4-Bit Integers

    COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION

  9. P2661 信息传递 TODO-TARJAN算法

    http://www.cnblogs.com/zbtrs/p/5762788.html http://blog.csdn.net/loi_yzs/article/details/52795093 都是 ...

  10. 【转】PHP框架性能测试报告 - ThinkPHP 3.2.3 Laravel 5.2 Yii2.0.5

    作为一个PHP开发者,而且是初创企业团队的技术开发者,选择开发框架是个很艰难的事情. 用ThinkPHP的话,招聘一个刚从培训机构出来的开发者就可以上手了,但是性能和后期代码解耦是个让人头疼的事情.不 ...