CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

David Hayden [MVP C#]

         .NET Tutorials, Patterns, and Practices

ActionFilterAttribute in ASP.NET MVC Framework

I remember Scott Guthrie mentioning the ability to add filters to your controller actions, so immediately after downloading the ASP.NET MVC Framework Preview 2 I opened up Reflector and found a new ActionFilterAttribute in System.Web.Mvc.

 

ActionFilterAttribute 

 

Not bothering to see if there is any documentation on it, I decided to give it a whirl with the new ASP.NET MVC Website Template:

 

ASP.NET MVC Framework

 

I pretended the About Action on the Home Controller required an SSL connection and Administrative privileges to be run and added a couple of custom ActionFilterAttributes to it:

 

HomeController

 

 

Here is the sample code for the RequiresSSL ActionFilterAttribute:

 

RequiresSSL ActionFilterAttribute 

 

Here is the sample code for the AdminOnly ActionFilterAttribute:

 

AdminOnly ActionFilterAttribute

 

The key here is that when I set filterContext.Cancel = true it seems to cancel the action so I take advantage of it.

Note that in the AdminOnlyAttribute I cast the filter.Controller property, which comes in as IController, to Controller for kicks. Obviously this cast could return null, so you may want to go the HttpContext route again to get the roles for the user. I just felt like writing the code a bit differently.

There is a lot more you can do with ActionFilterAttributes, but this was interesting in itself.

Hope this helps.

David Hayden

 



Comments

Christopher Steen said:

Sharepoint No VSE WSS for VS 2008 Until Summer [Via: Tariq ] WPF Podder v2 has been released! [Via:...

# March 6, 2008 2:03 AM

Mark said:

Hi, everything is ok, just those pictures with code are too big in both ff and ie, maybe you could try http://pastie.caboo.se/  to show your code snippets or some similar page. Thank you

# March 6, 2008 3:05 AM

David Hayden said:

I made the images smaller so you can see them in the browser fine. Hope that helps.

Regards, Dave

# March 6, 2008 9:00 AM

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# March 6, 2008 11:54 AM

Corey Gaudin said:

As a .NET developer, I am sure most of you have been keeping up with this years Mix '08. There are

# March 7, 2008 4:30 PM

Bolik said:

如何使用ASP.NET MVC Framework Preview 2 中ActionFilterAttribute属性设计Form用户验证

# March 9, 2008 4:25 AM

ScottGu's Blog said:

I'm slowly recovering from keynoting at MIX last week, and have been digging my way out of backlogged

# March 14, 2008 3:06 AM

BusinessRx Reading List said:

I'm slowly recovering from keynoting at MIX last week, and have been digging my way out of backlogged

# March 14, 2008 3:28 AM

Mirrored Blogs said:

I'm slowly recovering from keynoting at MIX last week, and have been digging my way out of backlogged

# March 14, 2008 4:06 AM

php code and scripts » Blog Archive » ActionFilterAttribute in ASP.NET MVC Framework said:

Pingback from  php code and scripts  » Blog Archive   » ActionFilterAttribute in ASP.NET MVC Framework

# March 14, 2008 5:04 PM

Enlaces: ASP.NET, ASP.NET AJAX, ASP.NET MVC y .NET « Thinking in .NET said:

Pingback from  Enlaces: ASP.NET, ASP.NET AJAX, ASP.NET MVC y .NET « Thinking in .NET

# March 16, 2008 7:15 PM

March 14th Links: ASP.NET, ASP.NET AJAX, ASP.NET MVC and .NET « .NET Framework tips said:

Pingback from  March 14th Links: ASP.NET, ASP.NET AJAX, ASP.NET MVC and .NET « .NET Framework tips

# March 17, 2008 2:48 AM

Mark said:

Hello your blog was userful for me. Thank you

# March 17, 2008 1:09 PM
Check out Devlicio.us!

This Blog

Syndication

News

CodeBetter.Com Home