Home | Projects | Tutorials | Articles | live chat | Submit Project | Big Vote
 
Ajax Projects
.NET Frameworks
Java Frameworks
PHP Frameworks
Ruby Frameworks
Other Frameworks
Cool AJAX sites
Ajax Resources
Ajax Tools
JavaScript frameworks

 Home /  Tutorials / ASP.NET MVC Action Filter - Ajax Only Attribute

ASP.NET MVC Action Filter - Ajax Only Attribute





The website I’m currently working on has a lot of AJAX incorporated in it, so my controller has a few actions which are only called in AJAX. At first I was using HttpContext.Request.IsAjaxRequest() within my action to control if the request was indeed an

Read The Full Tutorial.
































The website I’m currently working on has a lot of AJAX incorporated in it, so my controller has a few actions which are only called in AJAX. At first I was using HttpContext.Request.IsAjaxRequest() within my action to control if the request was indeed an Ajax one, but I didn’t like the fact that I needed to call HttpContext in my control; it also made a lot of redundant code. That is why I decided to build an Action Filter that would do that for me.

Here’s the Action Filter

   1. public class AjaxOnlyAttribute : ActionFilterAttribute 
   2. { 
   3.     public override void OnActionExecuting(ActionExecutingContext filterContext) 
   4.     { 
   5.         if(!filterContext.HttpContext.Request.IsAjaxRequest()) 
   6.             filterContext.HttpContext.Response.Redirect("/error/404"); 
   7.     } 
   8.  
   9.     public override void OnActionExecuted(ActionExecutedContext filterContext) 
  10.     { 
  11.  
  12.     } 
  13. } 

    public class AjaxOnlyAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if(!filterContext.HttpContext.Request.IsAjaxRequest())
                filterContext.HttpContext.Response.Redirect("/error/404");
        }

        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {

        }
    }

And an implementation example
view plaincopy to clipboardprint?

   1. [AjaxOnly] 
   2. public ActionResult AjaxActionMethod() 
   3. { 
   4.     .... 
   5. } 

    [AjaxOnly]
    public ActionResult AjaxActionMethod()
    {
        ....
    }

This might not be the best solution, but works pretty good for me so far!

source: helios

AddThis Social Bookmark Button
Top Projects
MSN Web Messenger
MessengerFX
ebuddy
e-messenger
ILoveIM
You Tube
AJAX file upload
KoolIM.com
Meebo
Ajax.NET Professional
Tutorials
JavaScript Basics
Using ASP.Net / AJAX slide extender to create a color selector
Logging In With YUI and CakePHP AuthComponent
Ajaxed: Ajax for classic ASP
Encrypt your AJAX traffic using Microsoft Silverlight and Ajax.NET Professional
Mastering Ajax Part 1 : Introduction to Ajax
True JavaScript Hash Table
Get Live User Interaction on Your BuddyPress Site With Ajax Chat
Building an Ajax Chat Room with the Ajax Transaction
Create a jQuery calendar with AJAX, PHP, and a remote data source