在這個例子中,我將使用一個實現圖像爲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