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 ...
随机推荐
- Asp.Net 之字符串和集合的使用
一:object:所有类的基类,所有类都直接或者间接继承自object 二:string 字符串的定义:string str=”” string str=new string(new char[ ...
- array(1) { [0]=> int(5) }和array(1) { [0]=> string(1) "5" }
php array数组: $arrayValue = array(5); $arrayValue = array('5'); 的不同之处 一个是整型一个是字符串型 array(1) { [0]=> ...
- 一图解析 React组件生命周期 (React Component Lifecycle)
React LifeCycle v1 参考官方文档作成 可放大 参考:https://reactjs.org/docs/react-component.html 数字补丁数字补丁数字补丁数字补丁数 ...
- hdu 1026 Ignatius and the Princess I 搜索,输出路径
Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- bnu 10805 矩形神码的 平面向量的运行
矩形神码的 Time Limit: 1000ms Memory Limit: 65536KB Special Judge 64-bit integer IO format: %lld J ...
- Effective C++ .47 traits与模板特化
#include <iostream> #include <cstdlib> #include <string> using namespace std; temp ...
- sublime Text3汉化和激活注册码
sublimeText3 很不错,前面几天下了vscore学习Node.js,感觉有点懵,今天下载sublimeText3,遇到的一些小问题,在这里说说: 百度云:https://pan.baidu. ...
- Install MySQL on Mac
1. 可参考此文章:http://www.cnblogs.com/macro-cheng/archive/2011/10/25/mysql-001.html 2. 目前MySQL(我用的mysql 5 ...
- Socket(套接字) IP TCP UDP HTTP
Socket(套接字) 阮老师的微博 (转)什么是套接字(Socket)? 应用层通过传输层进行数据通信时,TCP和UDP会遇到同时为多个应用程序进程提供并发服务的问题.多个TCP连接或多个应用程序进 ...
- 使用@Value进行静态常量的值注入
@Component public class ExpressConstant { public static String URL; @Value("${express.url}" ...