webapi的controller和action的控制。

使用场景:webapi接收到加密数据以及签名。验证签名是否有效。我们不能一个个action增加判断。

所以添加Filter是比较明智的方法。

首先 签名过滤器

namespace API.Filters
{
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = true, AllowMultiple = true)]
public class APISignAttribute : ActionFilterAttribute
{
public static readonly string APISign = WebConfigurationManager.AppSettings["APISign"]; public override void OnActionExecuting(HttpActionContext actionContext)
{ if (IsVaild(actionContext))
{
base.OnActionExecuting(actionContext);
}
else
{
throw new Exception("Invalid sign");
} } public bool IsVaild(HttpActionContext actionContext)
{
var sign = HttpContext.Current.Request.Form["data"].ToString();
//开始判断逻辑 return false;
} public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
// 若发生例外则不在这边处理
if (actionExecutedContext.Exception != null)
return; base.OnActionExecuted(actionExecutedContext);
}
}
}

异常过滤器

public override void OnException(HttpActionExecutedContext actionExecutedContext)
{
base.OnException(actionExecutedContext); var result = new apiResult<object>()
{
code = HttpStatusCode.BadRequest,
msg = actionExecutedContext.Exception.Message
};
if (actionExecutedContext.Exception is InvalidTokenException)
{
result.code = HttpStatusCode.Unauthorized;
}
string msg = "\r\n" + "apiErrorHandelattribute.StackTrace:\r\n" + actionExecutedContext.Exception.StackTrace + "\r\n\r\n" + "Message:\r\n" + actionExecutedContext.Exception.Message + "\r\n\r\n";
LogerHelper.WriteLog(msg); // 重新打包回传的讯息
actionExecutedContext.Response = actionExecutedContext.Request.CreateResponse(result.code, result); }

然后是启用方式,有2种

1 全局控制 在webapiConfig中添加

config.Filters.Add(new APISignAttribute());
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
); //注册 sign统一验证
config.Filters.Add(new APISignAttribute()); //注册 api异常处理
//config.Filters.Add(new ApiErrorHandleAttribute());
}
}

2 局部的控制

加在action上代表需要进入filter

加在controller上,代表该controller中所有action都要进入filter

[APISignAttribute]
public class TestController : BaseControllerAPI
{
[HttpPost]
public dynamic Get()
{
apiResult<dynamic> result = new apiResult<dynamic>();
result.data= new List<string>() { "", "" }; return result; } [APISignAttribute]
[HttpGet]
public dynamic haha()
{
return "value1";
}
}

结束

webapi Filter的更多相关文章

  1. webapi filter过滤器中获得请求的方法详情(方法名,Attributes)

    public class GlobalActionFilter : ActionFilterAttribute { private string _requestId; public override ...

  2. 20、ASP.NET MVC入门到精通——WebAPI

    本系列目录:ASP.NET MVC4入门到精通系列目录汇总 微软有了Webservice和WCF,为什么还要有WebAPI? 用过WCF的人应该都清楚,面对那一大堆复杂的配置文件,有时候一出问题,真的 ...

  3. MVC—WebAPI(调用、授权)

    ASP.NET MVC—WebAPI(调用.授权)   本系列目录:ASP.NET MVC4入门到精通系列目录汇总 微软有了Webservice和WCF,为什么还要有WebAPI? 用过WCF的人应该 ...

  4. WebAPI的压缩

    看见老外写了一篇ASP.NET Web API GZip compression ActionFilter with 8 lines of code 说实话被这标题吸引了,8行代码实现GZip压缩过滤 ...

  5. WebAPI性能优化之压缩解压

    有时候为了提升WebAPI的性能,减少响应时间,我们会使用压缩和解压,而现在大多数客户端浏览器都提供了内置的解压支持.在WebAPI请求的资源越大时,使用压缩对性能提升的效果越明显,而当请求的资源很小 ...

  6. WebAPI性能优化

    WebAPI性能优化之压缩解压 有时候为了提升WebAPI的性能,减少响应时间,我们会使用压缩和解压,而现在大多数客户端浏览器都提供了内置的解压支持.在WebAPI请求的资源越大时,使用压缩对性能提升 ...

  7. c#权限验证

    在开发过程中,需要对访问者的身份做权限验证(再filter中进行权限过滤). 在每次进入控制器方法之前进行调用:如 [ControllerAuth] [RoutePrefix("Clinic ...

  8. asp.net web api 授权功能

    1.重写授权方法 using System; using System.Collections.Generic; using System.Linq; using System.Net; using ...

  9. SSM+Redis+Shiro+Maven框架搭建及集成应用

    引文: 本文主要讲述项目框架搭建时的一些简单的使用配置,教你如何快速进行项目框架搭建. 技术: Spring+SpringMVC+Mybatis+Redis+Shiro+Maven          ...

随机推荐

  1. mysql无法远程连接到数据库解决方法

    ERROR 1130: Host ’xxx.xxx.xxx.xxx′ is not allowed to connect to this MySQL server这是告诉你没有权限连接指定IP的主机, ...

  2. php中按值传递和按引用传递的一个问题

    php中传递变量默认是按照值传递. 简单举个例子: <?php function testArray($arr){// &$arr $arr = array(1,2,3,); } $ar ...

  3. (转)deb制作文件详解

    转自:http://blog.chinaunix.net/uid-16184599-id-3041024.html 如何制作Deb包和相应的软件仓库,其实这个很简单.这里推荐使用dpkg来进行deb包 ...

  4. Ionic 安装JPush过程

    1.在官网注册App帐号,完成后会生成对应的AppKey 2. 进行在线安装 cordova plugin add https://github.com/jpush/jpush-phonegap-pl ...

  5. [Oracle][Partition][Controlfile]Partition 操作是否和 Controlfile有关?

    Partition 操作是否和 Controlfile有关? 通过实验来判断: 对比 Partition 前后的操作,看看controlfile 的dump 信息中是否有记录,结果发现没有记录在 co ...

  6. 2011 noip 提高组

    首先吐槽:刚刚写着写着突然蓝屏了,,emmm,写到最后一题了蓝屏了. 当时我的内心是崩溃的. 然后,旁边的大佬默默来了一句:论保存草稿的重要性. 连着蓝了三次之后开了防火墙,然后,,我左边那位同学又开 ...

  7. 使用canvas实现一个圆球的触壁反弹

    HTML <canvas id="canvas" width="500" height="500" style="borde ...

  8. rsync同步时,删除目标目录比源目录多余文件的方法(--delete)

    在日常运维工作中,我们经常用到rsync这个同步神器.有时在同步两个目录时,会要求删除目标目录中比源目录多出的文件,这种情况下,就可用到rsync的--delete参数来实现这个需求了. 实例说明:在 ...

  9. Scrum Meeting NO.6

    Scrum Meeting No.6 1.会议内容 今晚是提交编译测试程序的截至日期,大家果断都在忙着写编译,所以今天的进度不大. 2.任务清单 徐越 序号 近期的任务 进行中 已完成 1 代码重构: ...

  10. Leetcode——66.加一

    @author: ZZQ @software: PyCharm @file: leetcode66_加一.py @time: 2018/11/29 16:07 要求:给定一个由整数组成的非空数组所表示 ...