MVC 应用程序级别捕捉异常
捕捉异常:
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 应用程序级别捕捉异常的更多相关文章
- .NET [MVC] 利用特性捕捉异常
声明:本代码只适用于.NET MVC. 先创建一个类继承ActionFilterAttribute这个抽象类以及实现IExceptionFilter接口,并实现它的方法OnException. 代码如 ...
- .NET Core[MVC] 利用特性捕捉异常
声明:本方式适用于MVC.本代码只适用于.NET Core MVC. 先创建一个类继承ExceptionFilterAttribute这个抽象类,并override它的方法OnException. 代 ...
- C#程序如何捕捉未try/catch的异常——不弹“XXX已停止工作”报错框
诚意满满直接上代码: static void Main(string[] args) { //Main函数中增加此句 AppDomain.CurrentDomain.UnhandledExceptio ...
- [渣译文] 使用 MVC 5 的 EF6 Code First 入门 系列:为ASP.NET MVC应用程序创建更复杂的数据模型
这是微软官方教程Getting Started with Entity Framework 6 Code First using MVC 5 系列的翻译,这里是第六篇:为ASP.NET MVC应用程序 ...
- [渣译文] 使用 MVC 5 的 EF6 Code First 入门 系列:为ASP.NET MVC应用程序处理并发
这是微软官方教程Getting Started with Entity Framework 6 Code First using MVC 5 系列的翻译,这里是第十篇:为ASP.NET MVC应用程序 ...
- ASP.NET MVC应用程序处理并发
为ASP.NET MVC应用程序处理并发 2014-05-14 08:37 by Bce, 694 阅读, 2 评论, 收藏, 编辑 这是微软官方教程Getting Started with Enti ...
- 为ASP.NET MVC应用程序创建更复杂的数据模型
为ASP.NET MVC应用程序创建更复杂的数据模型 2014-05-07 18:27 by Bce, 282 阅读, 1 评论, 收藏, 编辑 这是微软官方教程Getting Started wit ...
- MVC应用程序请求密码的功能1
MVC应用程序请求密码的功能(一) 经过一系列的练习,实现了会员注册<MVC会员注册>http://www.cnblogs.com/insus/p/3439599.html,登录<M ...
- 使用Metrics.NET 构建 ASP.NET MVC 应用程序的性能指标
通常我们需要监测ASP.NET MVC 或 Web API 的应用程序的性能时,通常采用的是自定义性能计数器,性能计数器会引发无休止的运维问题(损坏的计数器.权限问题等).这篇文章向你介绍一个新的替代 ...
随机推荐
- 通过SEP禁用USB
1 Introduction 1.1 Scope This document provides comprehensive information of the reinforce ...
- Symantec更新服务器
HTTP liveupdate.symantec.com liveupdate.symantecliveupdate.com FTP update.symantec.com/opt/content ...
- Setting property 'source' to 'org.eclipse.jst.jee.server
警告: [SetPropertiesRule] Setting property 'source' to 'org.eclipse.jst.jee.server:project' did not fi ...
- CodeForces 625A 思维
题意是说一个人喝酒 有两种办法 买塑料瓶的 a块钱 喝了就没了 或者是买玻璃瓶的b块钱 喝完还能卖了瓶子c块钱 求最多能喝多少瓶 在开始判断一次 a与b-c的关系 即两种方式喝酒的成本 如果a< ...
- Mininet实验 使用l2_multi模块寻找最短路径实验
参考:使用l2_multi模块寻找最短路径实验 1. 实验目的 1.认识VND并且掌握其基本使用方法. 2.学会使用pox控制器的l2_multi模块寻找主机间的最短传输路径. 2. 实验原理 VND ...
- GDC2016【彩虹六号:围攻 】使丰富的“突破”成为可能的破坏系统
[彩虹六号:围攻 ]使得丰富的“突破”成为可能的破坏系统 深奥的战术游戏的背景下,最新的程序化破坏技术[REALBLAST]! 进行演讲的是Ubisoft Montreal的Julien ...
- PHP报错: Can't use method return value in write context
$dp_id = $this->getParam('dpId'); if(!empty($this->getParam('dpId'))) { $this->smarty->a ...
- iOS中利用CoreTelephony获取用户当前网络状态(判断2G,3G,4G)
前言: 在项目开发当中,往往需要利用网络.而用户的网络环境也需要我们开发者去注意,根据不同的网络状态作相应的优化,以提升用户体验. 但通常我们只会判断用户是在WIFI还是移动数据,而实际上,移动数据也 ...
- php-->mongodb[curd操作]
<?php /** * PHP操作MongoDB学习笔记 */ //************************* //** 连接MongoDB数据库 **// //********* ...
- mysql控制台命令
* mysql 链接服务器 mysql -h localhost -u root -p show processlist; update mysql.user set authentication_ ...