zoukankan      html  css  js  c++  java
  • 【原创】.NET Web API之filter ActionFilterAttribute 过滤器使用

    1、在filter类里面引用,与MVC里面的不同

    using System.Web.Http.Controllers;
    using System.Web.Http.Filters;

    2、filter类里面实现的代码,返回json

        public class FilterAttribute1 : ActionFilterAttribute
        {
            public override void OnActionExecuting(HttpActionContext filterContext)
            {
    
                var httpContext = (HttpContextWrapper)filterContext.Request.Properties["MS_HttpContext"];
                string ID = httpContext.Request["ID"];
                if (!string.IsNullOrEmpty(ID))
                {
                    string jsonString = "{"Status":0,"msg":"hello"}";
                    HttpResponseMessage result =
                        new HttpResponseMessage { Content = new StringContent(jsonString, Encoding.GetEncoding("UTF-8"), "application/json") };
                    filterContext.Response = result;
                }
                base.OnActionExecuting(filterContext);
            }
        }

    3、在Controller里面Action里面使用,和MVC一样

    // GET api/values
            [FilterAttribute1]
            public IEnumerable<string> Get()
            {
                return new string[] { "value1", "value2" };
            }
  • 相关阅读:
    接口自动化架构-获取用例
    Windows性能监控工具Perfmon使用指南
    接口自动化架构1-setting
    多进程
    线程锁、守护线程
    多线程
    xlrd模块
    封装写日志的类
    封装redis
    继承
  • 原文地址:https://www.cnblogs.com/hycms/p/4777805.html
Copyright © 2011-2022 走看看