Skip to content

Instantly share code, notes, and snippets.

@MMF
Created March 5, 2017 08:17
Show Gist options
  • Save MMF/8db2d7952a19d1390f3982d8734b4f3a to your computer and use it in GitHub Desktop.
Save MMF/8db2d7952a19d1390f3982d8734b4f3a to your computer and use it in GitHub Desktop.
AjaxOnly Attribute fot ASP.NET MVC Controller's method
using System.Reflection;
using System.Web.Mvc;
namespace SolutionName.Helpers
{
// request must be ajax
// used with controller methods
public class AjaxOnlyAttribute : ActionMethodSelectorAttribute
{
public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo)
{
return controllerContext.RequestContext.HttpContext.Request.IsAjaxRequest();
}
}
}
/*
How to use ?
- write [AjaxOnly] before the method
[AjaxOnly]
public JsonResult MethodName()
{
...
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment