Created
March 5, 2017 08:17
-
-
Save MMF/8db2d7952a19d1390f3982d8734b4f3a to your computer and use it in GitHub Desktop.
AjaxOnly Attribute fot ASP.NET MVC Controller's method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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