ozsenegal
ozsenegal

Reputation: 4133

ASP.NET MVC Custom HttpHandler

I'm follow this tutorial to use CAPTCHA in MVC application: http://coderjournal.com/2008/03/actionfilterattribute-aspnet-mvc-captcha/

I think this was made to ealry versions of ASP.NET MVC,because it doenst work for me.

Problem is that captcha image never shows,cause the custom handler (captcha.ashx) never gets the request.

In web.config:

<httpHandlers>
            <remove verb="*" path="*.asmx"/>
            <add verb="GET" validate="false" path="captcha.ashx" type="SCE.Models.Helpers.Captcha.CaptchaHandler"/>
        </httpHandlers>

The handler:

namespace SCE.Models.Helpers.Captcha
{
    public class CaptchaHandler : IHttpHandler
    {
        public bool IsReusable
        {
            get { return true; }
        }
        public void ProcessRequest(HttpContext context)
        {
            string guid = context.Request.QueryString["guid"];
            CaptchaImage ci = CaptchaImage.GetCachedCaptcha(guid);

            if (String.IsNullOrEmpty(guid) || ci == null)
            {
                context.Response.StatusCode = 404;
                context.Response.StatusDescription = "Not found";
                context.ApplicationInstance.CompleteRequest();
                return;
            }

            using (Bitmap b = ci.RenderImage())
            {
                b.Save(context.Response.OutputStream, ImageFormat.Gif);
            }
            context.Response.ContentType = "image/png";
            context.Response.StatusCode = 200;
            context.Response.StatusDescription = "OK";
            context.ApplicationInstance.CompleteRequest();
        }
    }
}

It seem newer version of MVC has changed the way they route handler,but i dont know what exactly...

Any ideias to call the handler?

UPDATE: Here is the solution ive found: In global.asax file:

routes.IgnoreRoute("{filename}.ashx/{*pathInfo}");

Upvotes: 0

Views: 4080

Answers (1)

Dustin Laine
Dustin Laine

Reputation: 38503

Using MVC 2, and ReCaptcha this was a pretty elegant solution. IMHO

http://devlicio.us/blogs/derik_whittaker/archive/2008/12/02/using-recaptcha-with-asp-net-mvc.aspx

Upvotes: 1

Related Questions