Advertisement
Willcode4cash

Ajax only method access

Aug 10th, 2016
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.63 KB | None | 0 0
  1. namespace MyApp.Web.Models
  2. {
  3.     using System;
  4.     using System.Web.Mvc;
  5.  
  6.     [AttributeUsage(AttributeTargets.Method)]
  7.     public class AjaxOnlyAttribute : ActionFilterAttribute
  8.     {
  9.         public override void OnActionExecuting(ActionExecutingContext filterContext)
  10.         {
  11.             if (!filterContext.HttpContext.Request.IsAjaxRequest())
  12.             {
  13.                 filterContext.HttpContext.Response.StatusCode = 404;
  14.                 filterContext.Result = new HttpNotFoundResult();
  15.             }
  16.             else
  17.             {
  18.                 base.OnActionExecuting(filterContext);
  19.             }
  20.         }
  21.     }
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement