我已经在互联网上搜索了一个few threads出现在stackoverlow中,关于在mvc中实现recaptcha遵循这个tutorial。但是,我发现一步一步的指令非常缺乏恕我直言。我已经下载了源文件。这是我的问题。
非常感谢任何帮助。
答案 0 :(得分:2)
好的,我明白了。这个tutorial非常有用,因为我在上一篇文章中提到的那篇文章没有做好解释。以下是我的电话代码:
的ContactController:
[CaptchaValidator]
[HttpPost]
public ActionResult ContactForm(ContactModels model, bool captchaValid)
{
if (!captchaValid)
{
ModelState.AddModelError("captcha", "You did not type the verification word correctly. Please try again.");
}
else if(ModelState.IsValid)
{
MailMessage netMessage = new MailMessage();
SmtpClient mailClient = new SmtpClient();
try
{
netMessage.From = new MailAddress("contact@myComp.com");
netMessage.To.Add(new MailAddress(model.email));
netMessage.IsBodyHtml = true;
netMessage.Priority = MailPriority.High;
netMessage.Subject = "Subject: " + model.subject;
netMessage.Body = getMailBody(model.fstName, model.lstName, model.subject, model.phone, model.email, model.address, model.apartment, model.city, model.state, model.zipcode, model.country, model.comments);
mailClient.Send(netMessage);
}
catch (Exception error)
{
Response.Write("Error sending email: " + error.Message + "<br /> StackTrace: " + error.StackTrace);
}
finally
{
netMessage.Dispose();
netMessage = null;
mailClient = null;
}
return RedirectToAction("Index", "Home");
}
return View();
}
这是我的观点:
@using MvcReCaptcha.Helpers;
@{
ViewBag.Title = "Contact";
Layout = "~/Views/Shared/_FullPage.cshtml";
}
<h2>Contact Us</h2>
@using (Html.BeginForm())
{
@Html.LabelFor(model => model.fstName)
@Html.TextBoxFor(model => model.fstName)
@Html.ValidationMessageFor(model => model.fstName)
@Html.LabelFor(model => model.lstName)
@Html.TextBoxFor(model => model.lstName)
@Html.ValidationMessageFor(model => model.lstName)
@Html.LabelFor(model => model.phone)
@Html.TextBoxFor(model => model.phone)
@Html.LabelFor(model => model.email)
@Html.TextBoxFor(model => model.email)
@Html.LabelFor(model => model.address)
@Html.TextBoxFor(model => model.address)
@Html.LabelFor(model => model.apartment)
@Html.TextBoxFor(model => model.apartment)
@Html.LabelFor(model => model.city)
@Html.TextBoxFor(model => model.city)
@Html.LabelFor(model => model.state)
@Html.TextBoxFor(model => model.state)
@Html.LabelFor(model => model.zipcode)
@Html.TextBoxFor(model => model.zipcode)
@Html.LabelFor(model => model.country)
@Html.TextBoxFor(model => model.country)
@Html.LabelFor(model => model.subject)
@Html.TextBoxFor(model => model.subject)
@Html.LabelFor(model => model.comments)
@Html.TextAreaFor(model => model.comments)
@Html.Raw(Html.GenerateCaptcha())
@Html.ValidationMessage("captcha")
<br />
<button type="submit">Submit</button>
}
注意到@ html.ValidationMessage中的参数与ModelStae.AddModelError中的参数匹配。我希望这可以帮助那些可能遇到同样问题的人。
答案 1 :(得分:0)
鉴于显示的逻辑,我将创建一个名为“Helpers”的新文件夹,因为看起来这些文件夹会扩展HtmlHelper,然后将文件放在那里。
将以下内容添加到您的web.config:
<appSettings>
<add key="ReCaptchaPrivateKey" value=" -- PRIVATE_KEY -- " />
<add key="ReCaptchaPublicKey" value=" -- PUBLIC KEY -- " />
</appSettings>
<namespaces>
<add namespace="MvcReCaptcha.Helpers"/>
</namespaces>
在您希望生成ReCaptcha的视图上放置以下内容:
@Using MvcReCaptcha.Helpers
@Html.GenerateCaptcha()
然后在与视图相同的控制器中添加前面的行以添加以下处理程序:
[CaptchaValidator]
[AcceptVerbs( HttpVerbs.Post )]
public ActionResult CreateComment( Int32 id, bool captchaValid )
{
if (!captchaValid)
{
ModelState.AddModelError("_FORM", "You did not type the verification word correctly. Please try again.");
}
else
{
// If we got this far, something failed, redisplay form
return View();
}
}
不要忘记您需要先注册才能获得公钥和私钥。 http://www.google.com/recaptcha/whyrecaptcha