AjaxOnlyAttribute.cs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. using System;
  2. using System.Web.Mvc;
  3. using Lottomat.Util.Extension;
  4. namespace Lottomat.Application.Admin
  5. {
  6. /// <summary>
  7. /// 仅允许Ajax操作
  8. /// </summary>
  9. [AttributeUsage(AttributeTargets.Method)]
  10. public class AjaxOnlyAttribute : ActionMethodSelectorAttribute
  11. {
  12. /// <summary>
  13. /// 跳过Ajax检测
  14. /// </summary>
  15. public bool Ignore { get; set; }
  16. /// <summary>
  17. /// 初始化仅允许Ajax操作
  18. /// </summary>
  19. /// <param name="ignore">跳过Ajax检测</param>
  20. public AjaxOnlyAttribute(bool ignore = false)
  21. {
  22. this.Ignore = ignore;
  23. }
  24. /// <summary>
  25. /// 验证请求有效性
  26. /// </summary>
  27. /// <param name="controllerContext">控制器上下文</param>
  28. /// <param name="methodInfo">方法</param>
  29. public override bool IsValidForRequest(ControllerContext controllerContext, System.Reflection.MethodInfo methodInfo)
  30. {
  31. if (Ignore)
  32. return true;
  33. return controllerContext.RequestContext.HttpContext.Request.IsAjaxRequest();
  34. }
  35. }
  36. }