Asp.Net Api2 过滤器的使用
2016-03-25 11:13
591 查看
1.注意:
apiController控制器 对应的过滤器System.Web.Http.Filters.ActionFilterAttribute的过滤器
MVC的Controller控制器 对应的过滤器System.Web.Http.Mvc.ActionFilterAttribute的过滤器
2.实例1
继承类ActionFilterAttribute
实例2(转):
3.抽象类ActionFilterAttribute定义
apiController控制器 对应的过滤器System.Web.Http.Filters.ActionFilterAttribute的过滤器
MVC的Controller控制器 对应的过滤器System.Web.Http.Mvc.ActionFilterAttribute的过滤器
2.实例1
继承类ActionFilterAttribute
public class FilterOneAttribute : ActionFilterAttribute { //执行前 public override void OnActionExecuting(HttpActionContext actionContext) { base.OnActionExecuting(actionContext); //返回403拒绝 HttpResponseMessage msg = actionContext.Request.CreateResponse(HttpStatusCode.Forbidden, "服务器拒绝了你的请求"); //设置相应对象,则不再执行Action actionContext.Response = msg; //返回500服务器错误 HttpResponseMessage msg = actionContext.Request.CreateResponse(HttpStatusCode.InternalServerError, "您还没有登录"); actionContext.Response = msg; //读取请求参数中的cookie HttpContextBase context = (HttpContextBase)actionContext.Request.Properties["MS_HttpContext"]; string cookie = string.Join(",", context.Request.Cookies.AllKeys); HttpResponseMessage msg = actionContext.Request.CreateResponse(HttpStatusCode.OK, cookie); actionContext.Response = msg; } }
实例2(转):
public class ActionFilter : ActionFilterAttribute { public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext actionContext) { base.OnActionExecuting(actionContext); //获取请求消息提数据 Stream stream = actionContext.Request.Content.ReadAsStreamAsync().Result; Encoding encoding = Encoding.UTF8; stream.Position = 0; string responseData = ""; using (StreamReader reader = new StreamReader(stream, encoding)) { responseData = reader.ReadToEnd().ToString(); } //反序列化进行处理 var serialize = new JavaScriptSerializer(); var obj = serialize.Deserialize<RequestDTO>(responseData); //在action执行前终止请求时,应该使用填充方法Response,将不返回action方法体。 if (obj == null) actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.OK, obj); if (string.IsNullOrEmpty(obj.PhoneType) || string.IsNullOrEmpty(obj.PhoneVersion) || string.IsNullOrEmpty(obj.PhoneID) || obj.StartCity < 1) { actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.OK, obj); } } }
3.抽象类ActionFilterAttribute定义
// // 摘要: // 表示所有操作筛选器特性的基类。 [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)] public abstract class ActionFilterAttribute : FilterAttribute, IActionFilter, IFilter { // // 摘要: // 初始化 System.Web.Http.Filters.ActionFilterAttribute 类的新实例。 protected ActionFilterAttribute(); // // 摘要: // 在调用操作方法之后发生。 // // 参数: // actionExecutedContext: // 操作执行的上下文。 public virtual void OnActionExecuted(HttpActionExecutedContext actionExecutedContext); public virtual Task OnActionExecutedAsync(HttpActionExecutedContext actionExecutedContext, CancellationToken cancellationToken); // // 摘要: // 在调用操作方法之前发生。 // // 参数: // actionContext: // 操作上下文。 public virtual void OnActionExecuting(HttpActionContext actionContext); public virtual Task OnActionExecutingAsync(HttpActionContext actionContext, CancellationToken cancellationToken); }
相关文章推荐
- ASP.NET过滤类SqlFilter,防止SQL注入[原创]_实用技巧_脚本之家
- 中国菜刀Cknife(C刀)自定义模式秒过安全狗. jsp过狗一句话,aspx过狗一句话.
- ASP.NET Web Service如何工作(3)
- ASP.NET Web Service如何工作(2)
- ASP.NET Web Service如何工作(1)
- asp.net ashx 一般处理程序 使用async await异步直接 copy可用哦
- 请求如何进入ASP.NET MVC框架
- ASP.NET Core 1.0中的管道-中间件模式
- ASP.NET过滤类SqlFilter,防止SQL注入 原创
- 你应该知道的 asp.net webform之异步页面
- 学习asp.net
- ASP.NET中的多线程整理
- 2016-AspNet-MVC教学-6-简单类Student的初次完整创建和使用
- Windows Iot:让Raspberry Pi跑起来(1)
- Asp.Net多线程用法1
- asp.net webform 中将前台显示出来的html标签解释为排版格式
- ASP.NET Core 1.0中的管道-中间件模式
- ASP.NET在底层类库中获取Session C#类中获取Session[原创]_实用技巧_脚本之家
- Raspberry 2 B 使用记录
- ASP.NET-文件上传代码