【问题标题】:http handler does no work in MVC IIS 8.0http 处理程序在 MVC IIS 8.0 中不起作用
【发布时间】:2013-09-14 00:40:16
【问题描述】:

我想在我的 mvc 网站中为我的验证码添加 http 处理程序并将其添加到 webconfig

     <system.webServer>

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

但是我的验证码图片没有显示,当我看到这个网址“http://localhost:2492/captcha.ashx”时,我收到了这个错误“找不到资源”

这是我的 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");
        }

怎么了?

更新: 这是我的处理程序实现:

 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)
        {

        }
    }
} 

【问题讨论】:

  • 验证码的实现是否位于同一个程序集 ManagedFusion.Web.Handlers.CaptchaImageHandler 中?如果没有,您的装配箱是本地的吗?您在类型字段中使用了弱名称。
  • 验证码在同一个程序集中实现
  • 您的处理程序实现是否具有带有 @WebHandler 指令的 ashx 文件?我能够在一个定义了该文件并且甚至没有 web.config 注册的 vanilla 项目中使用它。或者,您必须构建一个实例化 HttpHandler 的自定义 RouteHandler。
  • 编辑问题并添加处理程序实现

标签: asp.net asp.net-mvc asp.net-mvc-3 iis-7 asp.net-mvc-routing


【解决方案1】:

以防万一您想在此处使用 Mvc 路由,是 ManagedFusion.Web.CaptchaImageHandler 的自定义路由处理程序的实现。

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

然后,您在 RouteConfig.RegisterRoutes 中的路由定义定义自定义路由:

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

然后您可以删除 captcha.ashx 的 web.config httpHandler 子元素。

【讨论】:

  • 谢谢这是有效的。但我不知道我的解决方案是什么问题?
  • 我仍在调查这个事实。它与如何通过 SimpleHandlerFactory 执行 *.ashx 脚本映射有关。
猜你喜欢
  • 1970-01-01
  • 2014-11-05
  • 1970-01-01
  • 2016-08-21
  • 1970-01-01
  • 1970-01-01
  • 2013-02-15
  • 2011-12-18
  • 2011-05-14
相关资源
最近更新 更多