捕捉异常:

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. 使用 matlab 产生GK101任意波数据文件的方法

    一.引言 MATLAB是由美国mathworks公司发布的主要面对科学计算.可视化以及交互式程序设计的高科技计算环境.它不但包含高效的数值计算.数据处理能力,而且简单易用,是工程师日常研发过程中不可缺 ...

  2. HDU 1016 Prime Ring Problem(经典DFS+回溯)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  3. Transform-style和Perspective属性

    transform-style属性 transform-style属性是3D空间一个重要属性,指定嵌套元素如何在3D空间中呈现.他主要有两个属性值:flat和preserve-3d. transfor ...

  4. [转]在VS2010 VC++项目中引用Lib静态库(以Openssl为例)

    本文转自:http://kb.cnblogs.com/page/94467/ Openssl是个为网络通信提供安全及数据完整性的一种安全协议,囊括了主要的密码算法.常用的密钥和证书封装管理功能以及SS ...

  5. in_array效率问题以及解决办法

    先将要对比的数组中的 键  和  值  设置成一样 然后用isset() 进行对比,存在则删除

  6. Pull解析器学习

    1, package com.service; import java.io.InputStream; import java.io.OutputStream; import java.util.Ar ...

  7. 20145235李涛《Java程序设计》第一周学习总结

    教材学习内容总结 JAVA三大平台:Java SE(针对企业).Java EE(针对开发者) .Java ME(针对开发小型消费类电子产品) 其他 Java SE四个组成部分:JVM .JRE. JD ...

  8. CSS布局总结

    三种布局模型: 1.flow 标准流布局 2.float 浮动布局 3.layer 层叠布局 关于(flow) 标准流布局 浏览器默认的布局方式就是标准流布局.对于标准流布局下的的块元素和行内元素的特 ...

  9. 为什么数值类型byte取值范围是(-128~127)?

    在解决这个问题之前,我们先了解几个概念? 一.原码, 反码, 补码的概念 正数的反码和补码都与原码一样: 负数的反码.补码与原码不同,负数的反码:原码中除去符号位,其他的数值位取反,0变1,1变0.负 ...

  10. db link的查看创建与删除(转)

    1.查看dblink select owner,object_name from dba_objects where object_type='DATABASE LINK'; 或者 select * ...