Asp.net Web Api添加异常筛选器
一.定义一个异常筛选器
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http.Filters;
using System.Net;
using System.Net.Http;
namespace WebApi
{
public class NotImplExceptionFilter:ExceptionFilterAttribute
{
public override void OnException(HttpActionExecutedContext context)
{
if (context.Exception is NotImplementedException)
{
context.Response = new HttpResponseMessage(HttpStatusCode.NotFound);
}
}
}
}
注意:HttpStatusCode.NotFound 状态码只是随意定的,我这里
二.引用过滤器
(1)全局使用(对所有Action有效),在Global.asax中注册异常筛选器,如下
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
GlobalConfiguration.Configure(WebApiConfig.Register);
GlobalConfiguration.Configuration.Filters.Add(new NotImplExceptionFilter());
}
}
(2)指定Action使用
[NotImplExceptionFilter]
public IEnumerable<Contact> Get(string id = null)
{
...
throw new NotImplementedException("This method is not implemented");
}
三.控制器的Action加引用
public IEnumerable<Contact> Get(string id = null)
{
var thecontact= from contact in contacts
where contact.Id == id || string.IsNullOrEmpty(id)
select contact;
if (thecontact == null || thecontact.Count()==0)
{
var resp = new HttpResponseMessage(HttpStatusCode.NotFound)
{
Content = new StringContent(string.Format("No product with ID={0}", id)),
ReasonPhrase = "Product ID Not Found"
};
throw new NotImplementedException("This method is not implemented");
}
return thecontact;
}
Asp.net Web Api添加异常筛选器的更多相关文章
- ASP.NET Web API 文件產生器 - 使用 Swagger
转帖:http://kevintsengtw.blogspot.hk/2015/12/aspnet-web-api-swagger.html Swagger 是一套 API 互動文件產生器,使用 HT ...
- 为Asp.Net Web Api添加Http基本认证
Asp.net Web Api提供了RESTFul web服务的编程接口.默认RESTFul 服务没有提供任何验证或者基于角色的验证,这显然不适合Put.Post.Delete这些操作.Aps.net ...
- 为IIS Host ASP.NET Web Api添加Owin Middleware
将OWIN App部署在IIS上 要想将Owin App部署在IIS上,只添加Package:Microsoft.OWIN.Host.SystemWeb包即可.它提供了所有Owin配置,Middlew ...
- asp.net web api添加统一异常处理
1.自定义异常处理过滤器 /// <summary> /// 自定义异常处理过滤器 /// </summary> public class CustomExceptionFil ...
- ASP.NET Web API编程——异常捕获
1 向客户端发送错误消息 使用throw new HttpResponseException()向客户端抛出错误信息. HttpResponseException包含两个重载的构造函数,其中一个是构造 ...
- 自定义DelegatingHandler为ASP.NET Web Api添加压缩与解压的功能
HTTP协议中的压缩 Http协议中使用Accept-Encoding和Content-Encoding头来表示期望Response内容的编码和当前Request的内容编码.而Http内容的压缩其实是 ...
- ASP.NET Web API 安全筛选器
原文:https://msdn.microsoft.com/zh-cn/magazine/dn781361.aspx 身份验证和授权是应用程序安全的基础.身份验证通过验证提供的凭据来确定用户身份,而授 ...
- ASP.NET Web API 异常日志记录
如果在 ASP.NET MVC 应用程序中记录异常信息,我们只需要在 Global.asax 的 Application_Error 中添加代码就可以了,比如: public class MvcApp ...
- Asp.Net Web API 2第十二课——Media Formatters媒体格式化器
前言 阅读本文之前,您也可以到Asp.Net Web API 2 系列导航进行查看 http://www.cnblogs.com/aehyok/p/3446289.html 本教程演示如何在ASP.N ...
随机推荐
- IntelliJ使用指南—— 导入Eclipse的Web项目
通常一个团队中可能有人用eclipse,有人用intelliJ,那么经常会出现需要导入别人用eclipse建好的web项目.而IntelliJ提供了多种项目类型的导入方式,其中就有eclipse. 在 ...
- .net下 本地锁、redis分布式锁、zk分布式锁的实现
为什么要用锁? 大型站点在高并发的情况下,为了保持数据最终一致性就需要用到技术方案来支持.比如:分布式锁.分布式事务.有时候我们在为了保证某一个方法每次只能被一个调用者使用的时候,这时候我们也可以锁来 ...
- [javaSE] GUI(鼠标事件)
调用Button对象的addMouseListener方法,参数:MouseListener对象,这个类是个接口,需要实现以下方法 mouseClicked mousePressed mouseRel ...
- [LeetCode]Find Bottom Left Tree Value
Find Bottom Left Tree Value: Given a binary tree, find the leftmost value in the last row of the tre ...
- HDU 3306 Another kind of Fibonacci ---构造矩阵***
Another kind of Fibonacci Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Jav ...
- SQLServer数据库系统概念
数据模型是一种抽象模型,现实世界中的客观事物是彼此相互联系的 (1)数据模型是一组集成的概念,用户描述和操作组织内的数据,数据间的联系以及对数据的约束,它包含了数据结构,数据操作和完整性约束 (2)概 ...
- redux、immutablejs和mobx性能对比(二)
三.分析数据 1.前提说明 我对测试出的10个数据摘除最大值与最小值,然后求平均值 根据平均值我绘制了一个曲线图一个柱状图 曲线图用于查看1000-100000的性能趋势 柱状图用于比较在相同条数下r ...
- LeetCode算法题5----Longest Palindromic Substring
#5. Longest Palindromic Substring Given a string S, find the longest palindromic substring in S. You ...
- bootstrap 的模态框的宽与高设置
1,改变bootstrap 的宽与高, 将style=“height:900px”放在<div class = "modal-dialog">或者更外层上,整个模态框的 ...
- 【分享】BS大神的C++ 11 keynotes
看到infoQ上面有BS大神的keynotes讲C++ 11的,有点长,但是值得一看. http://www.infoq.com/presentations/Cplusplus-11-Bjarne ...