asp.net 图片验证码的HtmlHelper(2)


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcApplication1
{
public class BaseController:Controller
{
public ActionResult GetValidateCode(int length)
{
var vCode = new ValidateCode();
var code = vCode.CreateValidateCode(length);
Session["ValidateCode"] = code;
var bytes = vCode.CreateValidateGraphic(code);
return File(bytes, @"image/gif");
}
protected string GetValidateCode()
{
return Session["ValidateCode"].ToString();
}
}
}


4.让Controller继承BaseController:

复制代码 代码如下:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
namespace MvcApplication1.Controllers
{
public class HomeController :BaseController
{
public ActionResult Index()
{
ViewBag.Message = "Welcome to ASP.NET MVC!";
return View();
}
public ActionResult About()
{
var code = GetValidateCode();
return View();
}
}
}


5.页面调用代码:

复制代码 代码如下:


@using MvcApplication1
@{
ViewBag.Title = "About Us";
}
<h2>About</h2>
<p>
Put content here.
</p>
@Html.ValidateCode()


6.验证码的效果图:

asp.net 图片验证码的HtmlHelper


源码可以从这里下载: ValidateCode.rar

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjdssp.html