【问题标题】:Captcha not working验证码不起作用
【发布时间】:2012-04-30 12:25:28
【问题描述】:

我正在我的一个注册表单中实施验证码

在 Example.Cs 文件中我写了以下内容:

public static class CaptchaHelper
 {
  public static string Captcha(this HtmlHelper helper, string text)
       {
           string srcPath = System.Web.VirtualPathUtility.ToAbsolute("~/Handler1.ashx");
           string htmlContent = string.Empty;
           htmlContent += "<script type=\"text/javascript\">function __rc(){document.getElementById(\"" + text +
                          "\").src = \"../Handler1.ashx?query=\" + Math.random();}</script>";
           htmlContent += string.Format("<img id=\"{0}\" src=\"{1}\" alt=\"Captcha Image\"/>", text, srcPath);
           htmlContent += "<a href=\"#\" onclick=\"javascript:__rc();\">Reset</a>";
           return htmlContent;
       }
   }

在 View (.cshtml) 中我写了以下内容:

      @Html.Captcha("Sample") 

代替图像显示脚本可以帮助任何人。

谢谢 巴努

【问题讨论】:

    标签: asp.net-mvc-3 c#-4.0 razor entity-framework-4.1


    【解决方案1】:

    试试这个...

    public static class CaptchaHelper
     {
      public static MvcHtmlString Captcha(this HtmlHelper helper, string text)
           {
               string srcPath = System.Web.VirtualPathUtility.ToAbsolute("~/Handler1.ashx");
               string htmlContent = string.Empty;
               htmlContent += "<script type=\"text/javascript\">function __rc(){document.getElementById(\"" + text +
                              "\").src = \"../Handler1.ashx?query=\" + Math.random();}</script>";
               htmlContent += string.Format("<img id=\"{0}\" src=\"{1}\" alt=\"Captcha Image\"/>", text, srcPath);
               htmlContent += "<a href=\"#\" onclick=\"javascript:__rc();\">Reset</a>";
               return MvcHtmlString.Create(htmlContent.ToString());
           }
       }
    

    编辑...试试这行...

    htmlContent += string.Format("<img id=" + text + " src=" + srcPath + " alt=\"Captcha Image\"/>", text, srcPath);
    

    【讨论】:

    • 只获取标题和重置链接,请您帮忙
    • 同理 - 显示验证码标题和图像重置链接
    猜你喜欢
    • 1970-01-01
    • 2013-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多