在这个例子中,我将使用一个实现图像为MathCaptcha。在控制器你应该增加:
using CaptchaMvc.Attributes;
...
// At the action that process your form you have to add the [CaptchaVerify] atttribute
[HttpPost]
[CaptchaVerify("Captcha result is not valid.")] // Captcha is now able for validation
public ActionResult yourAction(FormCollection collection)
{
if (!ModelState.IsValid)
{
ViewBag.Error = "The captcha answer is not correct.";
return View("SameViewForm");
}
else
{
...
// Whatever you have to do when Captcha answer is correct.
return View();
}
}
在视图:
// Add
@using CaptchaMvc.HtmlHelpers
...
@using (Html.BeginForm())
{
...
<label style="color: red">@ViewBag.Error</label>
@Html.MathCaptcha()
<input type="submit" value="Send" />
}
你能粘贴图像是不显示的网址是什么?使用开发工具并检查该破碎的图像。 – mattytommo 2013-04-23 14:26:34
我是CaptchaMvc项目的开发者。确保你使用了MVC4库,如果问题仍然存在,请创建工作项目[here](http://captchamvc.codeplex.com/workitem/list/basic)。 – 2013-04-29 08:42:49
问题已解决...随着CaptchaMVC.dll,我还需要包含xml文件。我添加了使用Nuget的参考,它的工作原理 – Marcus25 2013-05-09 06:11:47