David Hayden [MVP C#]

Sponsors

The Lounge

News

  • CodeBetter.Com Home

Other Links

Teas

Patterns & Practices

Florida .NET Developer

Book Reviews

Tampa ASP.NET MVC Developer Group

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
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

 


Posted Wed, Mar 5 2008 6:16 PM by David Hayden

[Advertisement]

Comments

Christopher Steen wrote Link Listing - March 5, 2008
on Thu, Mar 6 2008 2:03 AM

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

Mark wrote re: ActionFilterAttribute in ASP.NET MVC Framework
on Thu, Mar 6 2008 3:05 AM

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

David Hayden wrote re: ActionFilterAttribute in ASP.NET MVC Framework
on Thu, Mar 6 2008 9:00 AM

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

Regards, Dave

DotNetKicks.com wrote New ActionFilterAttribute in ASP.NET MVC Framework
on Thu, Mar 6 2008 11:54 AM

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

Corey Gaudin wrote .NET Developers Heaven: Mix 08 Information Overload!
on Fri, Mar 7 2008 4:30 PM

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

Bolik wrote How to use Asp.Net Mvc ActionFilterAttribute for form authentication
on Sun, Mar 9 2008 4:25 AM

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

ScottGu's Blog wrote March 14th Links: ASP.NET, ASP.NET AJAX, ASP.NET MVC and .NET
on Fri, Mar 14 2008 3:06 AM

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

BusinessRx Reading List wrote March 14th Links: ASP.NET, ASP.NET AJAX, ASP.NET MVC and .NET
on Fri, Mar 14 2008 3:28 AM

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

Mirrored Blogs wrote March 14th Links: ASP.NET, ASP.NET AJAX, ASP.NET MVC and .NET
on Fri, Mar 14 2008 4:06 AM

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

php code and scripts » Blog Archive » ActionFilterAttribute in ASP.NET MVC Framework wrote php code and scripts » Blog Archive » ActionFilterAttribute in ASP.NET MVC Framework
on Fri, Mar 14 2008 5:04 PM

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

Enlaces: ASP.NET, ASP.NET AJAX, ASP.NET MVC y .NET « Thinking in .NET wrote Enlaces: ASP.NET, ASP.NET AJAX, ASP.NET MVC y .NET « Thinking in .NET
on Sun, Mar 16 2008 7:15 PM

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

March 14th Links: ASP.NET, ASP.NET AJAX, ASP.NET MVC and .NET « .NET Framework tips wrote March 14th Links: ASP.NET, ASP.NET AJAX, ASP.NET MVC and .NET « .NET Framework tips
on Mon, Mar 17 2008 2:48 AM

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

Mark wrote re: ActionFilterAttribute in ASP.NET MVC Framework
on Mon, Mar 17 2008 1:09 PM

Hello your blog was userful for me. Thank you