【asp精品源码栏目提醒】:网学会员--在 asp精品源码编辑为广大网友搜集整理了:一个图片验证码的HtmlHelper - 其它资料绩等信息,祝愿广大网友取得需要的信息,参考学习。
一个图片验证码的 HtmlHelper,原来的调用代码如下:封装成 HtmlHelper 后:Html.ValidateCode使用步骤如下:1.建一个验证码 Helperusing Systemusing System.Collections.Genericusing System.ComponentModelusing Systemusing System.Collections.Genericusing System.Diagnostics.CodeAnalysisusing System.Globalizationusing System.Linq.Expressionsusing System.Security.Policyusing System.Textusing System.Webusing System.Web.Mvcusing System.Web.Mvc.Resourcesusing System.Web.Routingnamespace MvcApplication1public static class ValidateCodeHelperprivate const string IdPrefix validateCodeprivate const int Length 4public static MvcHtmlString ValidateCodethis HtmlHelper helperreturn ValidateCodehelper IdPrefixpublic static MvcHtmlString ValidateCodethis HtmlHelper helperstringidreturn ValidateCodehelper id Lengthpublic static MvcHtmlString ValidateCodethis HtmlHelper helper stringid int lengthreturn ValidateCodehelper id length nullpublic static MvcHtmlString ValidateCodethis HtmlHelper helper stringid object htmlAttributesreturn ValidateCodehelper id Length htmlAttributespublic static MvcHtmlString ValidateCodethis HtmlHelper helper intlength object htmlAttributesreturn ValidateCodehelper IdPrefix length htmlAttributespublic static MvcHtmlString ValidateCodethis HtmlHelper helper objecthtmlAttributesreturn ValidateCodehelper 4 htmlAttributespublic static MvcHtmlString ValidateCodethis HtmlHelper helper intlengthreturn ValidateCodehelperlength nullpublic static MvcHtmlString ValidateCodethis HtmlHelper helperstringidint lengthobject htmlAttributesstring finalId id _imgValidateCodevar tagBuild new TagBuilderimgtagBuild.GenerateIdfinalIdvar defaultController RouteRouteTable.RoutesDefault.Defaultscontroller /var controller HttpContext.Current.Request.Url.Segments.Length 1 defaultController: HttpContext.Current.Request.Url.Segments1tagBuild.MergeAttributesrc string.Format/0GetValidateCodelength1controllerlengthtagBuild.MergeAttributealt 看不清?点我试试看!tagBuild.MergeAttributestylecursor:pointertagBuild.MergeAttributesAnonymousObjectToHtmlAttributeshtmlAttributesvar sb new StringBuildersb.Append return MvcHtmlString.CreatetagBuildsb.ToStringpublic static RouteValueDictionary AnonymousObjectToHtmlAttributesobject htmlAttributesvar result new RouteValueDictionaryif htmlAttributes nullforeach PropertyDescriptor property in TypeDescriptor.GetPropertieshtmlAttributesresult.Addproperty.Name.Replace_ - property.GetValuehtmlAttributesreturn result2.生成验证码的代码:using Systemusing System.Collections.Genericusing System.Linqusing System.Webusing System.Drawingusing System.Drawing.Drawing2Dusing System.IOusing System.Drawing.Imagingnamespace MvcApplication1public class ValidateCodepublic ValidateCode////// 验证码的最大长度///public int MaxLengthget return 10 ////// 验证码的最小长度///public int MinLengthget return 1 ////// 生成验证码//////指定验证码的长度///public string CreateValidateCodeint lengthvar randMembers new intlengthvar validateNums new intlengthvar validateNumberStr //生成起始序列值var seekSeek uncheckedintDateTime.Now.Ticksvar seekRand new RandomseekSeekvar beginSeek intseekRand.Next0 Int32.MaxValue - length 10000var seeks new intlengthfor var i 0 i length ibeginSeek 10000seeksi beginSeek//生成随机数字for var i 0 i length ivar rand new Randomseeksivar pownum 1 intMath.Pow10 lengthrandMembersi rand.Nextpownum Int32.MaxValue//抽取随机数字for var i 0 i length ivar numStr randMembersi.ToStringvar numLength numStr.Lengthvar rand new Randomvar numPosition rand.Next0 numLength - 1validateNumsi Int32.ParsenumStr.SubstringnumPosition 1//生成验证码for var i 0 i length ivalidateNumberStr validateNumsi.ToStringreturn validateNumberStr////// 创建