Tuesday, November 18, 2008

ASP.NET MVC Beta Captcha example

I was working on an ASP.NET MVC application (in vb.net) and I needed to include a captcha thingy.

Nick Berardi already made a captcha for ASP.NET MVC, so I used his implementation. But it didn't work right out the box. After some tweaking it was still not working. So Nick has emailed me some information and from that I've created a working sample that I'll post here.

I got this error when using the dll: Reference required to assembly 'System.Web.Mvc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' containing the base class 'System.Web.Mvc.ActionFilterAttribute'. Add one to your project.
This while I had the dll referenced, but probably it was refering to and older dll.
Anyway, it's fixed now.



These are the modifications I made (compared to the default asp.net mvc beta project template:

- I changed web.config and added this httphandler in the system.web section:


- I changed global.asax to add some routes to ignore:
routes.IgnoreRoute("{handler}.ashx")
routes.IgnoreRoute("{controller}/{handler}.ashx")
routes.IgnoreRoute("{controller}/{action}/{handler}.ashx")

- I've added a new project ManagedFusion with the .cs files that made/modified by Nick and this was referenced in the asp.net mvc project.

Click here to download the captcha demo (vb.net) project

Update: Instead of Html.CaptchaTextBox("caption") you can also use this code:
Html.TextBox("captcha", "", New With {.autocomplete = "off", .maxlength = "5"})

No comments: