Resources » .NET programming » ASP.NET/Web Applications

ASP.NET MVC Action filters


Posted Date: 20-Jun-2009  Last Updated:   Category: ASP.NET/Web Applications    
Author: Member Level: Gold    Points: 10



I love the really cool Action Filter’s in ASP.Net MVC. I use it to keep my code clean and reusable. This post is not about what is an Action Filter but is more about how to use them to have cleaner and reusable components.

As an example, consider an hypothetical application. The application needs to show a Home Page. The home page shows the company information. Now, if the user is logged in then the Home page is a home page with Member features. Hmm so well, I can just check for the authentication status and Redirect to the Member home page. Something like this,


[HandleError]
public class HomeController : Controller
{
public ActionResult Home()
{
if (Request.IsAuthenticated == true)
{
return RedirectToAction("Home", "Member");
}

return View();
}

public ActionResult Resources()
{
return View();
}
}


Works as expected. Sweet.

The next day I am told to do the same thing for the “Resources” page. If not logged in then default Resources will be shown and if logged in then Resource’s page specific for Members. So, as usual,


[HandleError]
public class HomeController : Controller
{
public ActionResult Home()
{
if (Request.IsAuthenticated == true)
{
return RedirectToAction("Home", "Member");
}

return View();
}

public ActionResult Resources()
{
if (Request.IsAuthenticated == true)
{
return RedirectToAction("Resources", "Member");
}

return View();
}
}


But wait! what if I have to do the same thing for some other page as well ? Would I be repeating the code always ? I should do this in a better way. Think Refactor !! Lightning strikes and a thought comes up. Why not use an Action filter to do this ? I would just have to Tag the filter to any ActionResult needing a redirection. That sounds good.
Here goes the refactored code,


[HandleError]
public class HomeController : Controller
{
[RedirectIfAuthenticated]
public ActionResult Home()
{
return View();
}

[RedirectTOIfAuthenticated]
public ActionResult Resources()
{
return View();
}
}


Doesn’t it look neater, more cleaner, simple and reusable ? Thanks !

BTW, here is the Action filter code,


public class RedirectIfAuthenticated : ActionFilterAttribute
{
public string RedirectionPath { get; set; }

public override void OnActionExecuting(ActionExecutingContext filterContext)
{
HttpContext ctx = HttpContext.Current;

if (ctx.Request.IsAuthenticated == true)
{
ctx.Response.Redirect(RedirectionPath);
}

base.OnActionExecuting(filterContext);
}
}


You might have noticed I have a property named “RedirectionPath”. By default it is set for Member Home redirection. This is in ANTICIPATION of FUTURE i.e. in case I need to redirect to some other page for a logged in user. For e.g., if the user is logged in then he should be shown a different About us page. As in this case we don’t have to show the Member home page so we will need to override the default “RedirectionPath” property value when using the RedirectIfAuthenticated Action filter.


[RedirectIfAuthenticated(RedirectionPath = CustomConstants.MemberAboutusPage)]
public ActionResult AboutUs()
{
return View();
}


where CustomConstants.MemberAboutusPage is a constant having the path to the AboutUs page like “/Member/AboutUs”

Have fun.


Reference: http://abitsmart.com/?p=55


Did you like this resource? Share it with your friends and show your love!

Responses to "ASP.NET MVC Action filters"

No responses found. Be the first to respond...

Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Submit Article     Return to Article Index

    Subscribe to Subscribers
    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Talk to Webmaster Tony John
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India