After working with ASP.Net MVC, and my previous posts, in this Post I am trying to implement a Captcha HTML Helper, an HTML Helper is just a method that returns a string. Creating Custom HTML Helpers. It is an extension method, to the existing HTML Helper class.
Here is code.(For more details about the Captcha generation code, check my previous post. : Captcha using ASP.Net and C#);
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;
}
}
And in the View you can use like, you may need to import the Namespace of CaptchaHelper class.
< %= Html.Captcha("Sample") %>
It will render a security image. Happy Programming
Pingback: ASP.NET MVC Archived Blog Posts, Page 1
Nice post buddy…
thanks