HttpHandler永远不会被调用

时间:2012-07-06 20:37:56

标签: asp.net-mvc captcha httphandler

我有一个MVC应用程序,我正在尝试添加ManagedFusion.Web.Captcha.CaptchaImageHandler,以便我可以编写以下代码:

<label for="captcha">Enter @Html.Raw(Business.Captcha.CaptchaImage(Html, 50, 180))  Below</label>

并显示图像。该类的代码只是从在线示例中剪切和粘贴:

public static string CaptchaImage(this HtmlHelper helper, int height, int width) {
            ManagedFusion.Web.Controls.CaptchaImage image = new ManagedFusion.Web.Controls.CaptchaImage {
                Height = height,
                Width = width,
            };
            HttpRuntime.Cache.Add(image.UniqueId, image,
                null,
                DateTime.Now.AddSeconds(ManagedFusion.Web.Controls.CaptchaImage.CacheTimeOut),
                Cache.NoSlidingExpiration,
                CacheItemPriority.NotRemovable,
                null);
            StringBuilder stringBuilder = new StringBuilder(256);
            stringBuilder.Append("<input type=\"hidden\" name=\"captcha-guid\" value=\"");
            stringBuilder.Append(image.UniqueId);
            stringBuilder.Append("\" />");
            stringBuilder.AppendLine();
            stringBuilder.Append("<img src=\"");
            stringBuilder.Append("/captcha.ashx?guid=" + image.UniqueId);
            stringBuilder.Append("\" alt=\"CAPTCHA\" width=\"");
            stringBuilder.Append(width);
            stringBuilder.Append("\" height=\"");
            stringBuilder.Append(height);
            stringBuilder.Append("\" />");
            return stringBuilder.ToString();
        }

我已将以下内容添加到我的web.config

<system.web>

<httpHandlers>
  <add  verb="GET" path="test.sample"  type="ManagedFusion.Web.Handlers.CaptchaImageHandler, ManagedFusion.Web.Captcha" validate="false" />
</httpHandlers>
</system.web>

<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<modules runAllManagedModulesForAllRequests="true" >

</modules>
<handlers>
  <add name="CaptchaImageHandler" verb="GET" path="captcha.ashx"  type="ManagedFusion.Web.Handlers.CaptchaImageHandler, ManagedFusion.Web.Captcha" />
</handlers>

所有以前的SO问题都指向system.web-&gt; httpHandlers被Cassini接收,而system.webServer-&gt;处理程序被IIS 7接收。但每当我导航到具有上述代码的视图时,我总是为/captcha.ashx获得404。 global.asax中没有路由忽略规则。这里发生了什么?这就像我所做的一样,无论是在我的本地机器上还是在已部署的IIS 7实例上都可以启动处理程序。

1 个答案:

答案 0 :(得分:4)

在Global.asax文件中,我需要在默认路由映射之前添加ignore路由,因此整个方法如下所示:

public static void RegisterRoutes(RouteCollection routes) {
        routes.Ignore("captcha.ashx");

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