简体   繁体   中英

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}");

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM