2010-08-26 73 views
0

我跟隨本教程中的MVC應用程序中使用驗證碼: http://coderjournal.com/2008/03/actionfilterattribute-aspnet-mvc-captcha/ASP.NET MVC自定義的HttpHandler

我認爲這是爲了ealry ASP.NET MVC的版本,因爲它doenst爲我工作。

問題是驗證碼圖像從不顯示,導致自定義處理程序(captcha.ashx)從未獲取請求。

在web.config中:

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

的處理程序:

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

這似乎MVC的新版本已經改變了他們路由處理,但我不知道究竟是什麼?

任何想法調用處理程序?

UPDATE: 這裏找到了解決辦法IVE: 在Global.asax文件:

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

您鏈接到了ASP.NET MVC預覽1或2。你甚至都不需要爲這個自定義HTTP處理程序編寫的代碼。你只是想在你的ASP.NET MVC網站中使用CAPTCHA嗎? – bzlm 2010-08-26 21:50:43

+0

[如何在asp.net mvc中使用驗證碼](http://stackoverflow.com/questions/2286688/how-to-use-captcha-in-asp-net-mvc) – bzlm 2010-08-26 21:51:52

+0

yes captcha in mvc。 ..但不recaptcha – ozsenegal 2010-08-26 21:54:37

回答

1

使用MVC 2,驗證碼,這是一個非常優雅的解決方案。恕我直言

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

+0

對不起,我不能使用recaptcha,因爲我已經嘗試,它可以爲我工作 – ozsenegal 2010-08-26 21:55:33

+0

您可以將這個相同的實現方法移植到您正在使用的任何驗證碼。我個人很喜歡用簡單的captcha屬性來裝飾我的動作。 – 2010-08-26 22:06:13