• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

asp.net 图片验证码的HtmlHelper

c# 搞代码 4年前 (2022-01-09) 21次浏览 已收录 0个评论

一个图片验证码的HtmlHelper,原来的调用代码如下:

<img id="validateCode" mailto:src='@Url.Action(%22GetValidateCode%22)'/> <script language="javascript" type="text/javascript"> $(document).ready(function () { $("#validateCode").bind("click", function () { var url = $(this).attr("src"); url += "?" + Math.random(); $(this).attr("src", url); }); }); </script>

封装成HtmlHelper后:
@Html.ValidateCode()
使用步骤如下:
1.建一个验证码Helper

using System; using System.Collections.Generic; using System.ComponentModel; using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.Linq.Expressions; using System.Security.Policy; using System.Text; using System.Web; using System.Web.Mvc; using System.Web.Mvc.Resources; using System.Web.Routing; namespace MvcApplication1 { public static class ValidateCodeHelper { private const string IdPrefix = "validateCode"; private const int Length = 4; public static MvcHtmlString ValidateCode(this HtmlHelper helper) { return ValidateCode(helper, IdPrefix); } public static MvcHtmlString ValidateCode(this HtmlHelper helper,string id) { return ValidateCode(helper, id, Length); } public static MvcHtmlString ValidateCode(this HtmlHelper helper, string id, int length) { return ValidateCode(helper, id, length, null); } public static MvcHtmlString ValidateCode(this HtmlHelper helper, string id, object htmlAttributes) { return ValidateCode(helper, id, Length, htmlAttributes); } public static MvcHtmlString ValidateCode(this HtmlHelper helper, int length, object htmlAttributes) { return ValidateCode(helper, IdPrefix, length, htmlAttributes); } public static MvcHtmlString ValidateCode(this HtmlHelper helper, object htmlAttributes) { return ValidateCode(helper, 4, htmlAttributes); } public static MvcHtmlString ValidateCode(this HtmlHelper helper, int length) { return ValidateCode(helper,length, null); } public static MvcHtmlString ValidateCode(this HtmlHelper helper,string id,int length,object htmlAttributes) { string finalId = id + "_imgValidateCode"; var tagBuild = new TagBuilder("img"); tagBuild.GenerateId(finalId); var defaultController = ((Route)RouteTable.Routes["Default"]).Defaults["controller"] + "/"; var controller = HttpContext.Current.Request.Url.Segments.Length == 1 ? defaultController : HttpContext.Current.Request.Url.Segments[1]; tagBuild.MergeAttribute("src", string.Format("/{0}GetValidateCode?length={1}",controller,length)); tagBuild.MergeAttribute("alt", "看不清?点我试试看!"); tagBuild.MergeAttribute("style","cursor:pointer;"); tagBuild.MergeAttributes(AnonymousObjectToHtmlAttributes(htmlAttributes)); var sb = new StringBuilder(); sb.Append("<script language=\"javascript\" type=\"text/javascript\">"); sb.Append("$(document).ready(function () {"); sb.AppendFormat("$(\"#{0}\").bind(\&quot<span style="color:transparent">来1源gaodai#ma#com搞*代#码1网</span>;click\", function () {{", finalId); //sb.Append("$(this).attr(\"style\", \"cursor:pointer;\");"); sb.Append("var url = $(this).attr(\"src\");"); sb.Append("url += \"&\" + Math.random();"); sb.Append("$(this).attr(\"src\", url);"); sb.Append("});"); sb.Append("});"); sb.Append("</script>"); return MvcHtmlString.Create(tagBuild+sb.ToString()); } public static RouteValueDictionary AnonymousObjectToHtmlAttributes(object htmlAttributes) { var result = new RouteValueDictionary(); if (htmlAttributes != null) { foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(htmlAttributes)) { result.Add(property.Name.Replace('_', '-'), property.GetValue(htmlAttributes)); } } return result; } } }

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:asp.net 图片验证码的HtmlHelper
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址