简体   繁体   中英

http handler does no work in MVC IIS 8.0

i want to add http handler for my captcha in my mvc website and add this to webconfig

     <system.webServer>

        <handlers>
          <add name="HandlerName"
                     path="captcha.ashx" verb="*" type="ManagedFusion.Web.Handlers.CaptchaImageHandler"
                     resourceType="Unspecified" />
        </handlers>
 </system.webServer>

but my Captcha Image does not show and when i see this url " http://localhost:2492/captcha.ashx " i get this error "The resource cannot be found"

this is my globals.asax

public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );


            routes.IgnoreRoute("{resource}/{any}.ashx");
            routes.IgnoreRoute("{any}.ashx");
            routes.IgnoreRoute("captcha.ashx");
        }

what's wrong ?

Update : this is my handler implementation :

 public class CaptchaImageHandler : IHttpHandler 
    {
        #region IHttpHandler Members

        /// <summary>
        /// Gets a value indicating whether another request can use the <see cref="T:System.Web.IHttpHandler"/> instance.
        /// </summary>
        /// <value></value>
        /// <returns>true if the <see cref="T:System.Web.IHttpHandler"/> instance is reusable; otherwise, false.</returns>
        public bool IsReusable
        {
            get { return true; }
        }

        /// <summary>
        /// Enables processing of HTTP Web requests by a custom HttpHandler that implements the <see cref="T:System.Web.IHttpHandler"/> interface.
        /// </summary>
        /// <param name="filterContext">An <see cref="T:System.Web.HttpContext"/> object that provides references to the intrinsic server objects (for example, Request, Response, Session, and Server) used to service HTTP requests.</param>
        public void ProcessRequest(HttpContext context)
        {
            // get the unique GUID of the captcha; this must be passed in via the querystring
            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.Response.End();
                return;
            }

            // write the image to the HTTP output stream as an array of bytes
            using (Bitmap b = ci.RenderImage())
            {
                b.Save(context.Response.OutputStream, ImageFormat.Gif);
            }

            context.Response.ContentType = "image/gif";
            context.Response.StatusCode = 200;
            context.Response.StatusDescription = "OK";
            context.Response.End();
        }

        #endregion

        public void Dispose()
        {
            throw new NotImplementedException();
        }

        public void Init(HttpApplication context)
        {

        }
    }
} 

Just in case you want to use the Mvc Routing here is an implementation of the custom route handler for ManagedFusion.Web.CaptchaImageHandler.

public class CaptchaRouteHandler:IRouteHandler 
{ 
    public System.Web.IHttpHandler GetHttpHandler(RequestContext requestContext) 
    { 
        return new ManagedFusion.Web.CaptchaImageHandler(); 
    }
}

Then, your route definition in RouteConfig.RegisterRoutes define the custom route:

public static void RegisterRoutes(RouteCollection routes)
{

    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.Add(
        "Captcha Route",
        new Route(
            "captcha.ashx",
            new CaptchaRouteHandler()));

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
}

You can then drop the web.config httpHandler child element for captcha.ashx.

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