You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AjaxOnlyAttribute.cs 1.3 KiB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using System;
  2. using System.Web.Mvc;
  3. namespace Learun.Application.Web
  4. {
  5. /// <summary>
  6. /// 版 本 Learun-ADMS-Ultimate V7.0.0 数字化智慧校园
  7. /// Copyright (c) 2013-2018 北京泉江科技有限公司
  8. /// 创建人:陈彬彬
  9. /// 日 期:2017.03.08
  10. /// 描 述:仅允许Ajax操作
  11. /// </summary>
  12. [AttributeUsage(AttributeTargets.Method)]
  13. public class AjaxOnlyAttribute : ActionMethodSelectorAttribute
  14. {
  15. /// <summary>
  16. /// 初始化仅允许Ajax操作
  17. /// </summary>
  18. /// <param name="ignore">跳过Ajax检测</param>
  19. public AjaxOnlyAttribute(bool ignore = false)
  20. {
  21. Ignore = ignore;
  22. }
  23. /// <summary>
  24. /// 跳过Ajax检测
  25. /// </summary>
  26. public bool Ignore { get; set; }
  27. /// <summary>
  28. /// 验证请求有效性
  29. /// </summary>
  30. /// <param name="controllerContext">控制器上下文</param>
  31. /// <param name="methodInfo">方法</param>
  32. public override bool IsValidForRequest(ControllerContext controllerContext, System.Reflection.MethodInfo methodInfo)
  33. {
  34. if (Ignore)
  35. return true;
  36. return controllerContext.RequestContext.HttpContext.Request.IsAjaxRequest();
  37. }
  38. }
  39. }