1

我搜索了互联网, stackoverlow 中出现了一些关于按照本教程在 mvc 中实现 recaptcha 的线程。但是,我发现分步说明非常缺乏恕我直言。我已经下载了源文件。这是我的问题。

  1. 我应该将 CaptchaValidatorAttribute.cs、AssemblyInfo.cs 和 GenerateCaptcha.cs 文件放在我的 MVC 文件夹中的什么位置?
  2. 将这些 .cs 文件放置在正确位置后,如何在 ContactUsController 和 ContactUs.cshtml 中引用它们?分步说明没有提到这一点。

任何帮助深表感谢。

4

2 回答 2

2

好的,我明白了。本教程非常有帮助,因为我在最初的帖子中提到的那个没有很好地解释。以下是我的编码:

联系人控制器:

[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 中的参数匹配。我希望这可以帮助可能遇到与我相同问题的人。

于 2013-08-06T15:23:26.210 回答
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

于 2013-08-05T20:41:32.733 回答