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" };
        }

 

相关文章:

  • 2021-11-19
  • 2021-12-23
  • 2021-11-21
  • 2021-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-24
  • 2022-02-10
猜你喜欢
  • 2022-12-23
  • 2021-10-19
  • 2021-09-27
  • 2022-12-23
  • 2021-12-23
  • 2022-12-23
  • 2021-12-23
相关资源
相似解决方案