2013-08-05 46 views
1

我已經搜索了互聯網和一個few threads出現在stackoverlow關於實施在mvc recaptcha通過以下tutorial。但是,我發現一步一步的說明非常缺乏恕我直言。我已經下載了源文件。這是我的問題。如何在MVC 4項目中實現recaptcha?

  1. 我該如何將CaptchaValidatorAttribute.cs,AssemblyInfo.cs和GenerateCaptcha.cs文件放置在我的MVC文件夾中?
  2. 將這些.cs文件放在正確的位置後,我如何在ContactUsController和ContactUs.cshtml中引用它們?一步一步的教學沒有提到這一點。

任何幫助,非常感謝。

+0

這些都是我的文件夾: 屬性, 參考, 的App_Data, App_Start, 內容, 控制器, 過濾器, 圖片, 模型, 腳本, 意見。 本教程沒有顯示它在View或Controller中使用時如何引用這些.cs文件。 – 2myCharlie

+0

NuGet ** Google reCAPTCHA V2 ** for MVC 4 and 5 - [NuGet Package](https://www.nuget.org/packages/reCAPTCH.MVC/) - [Demo And Document](http:///recaptchamvc.apphb.com/) – Sender

回答

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("[email protected]"); 
        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。我希望這可以幫助像我一樣可能遇到同樣問題的人。

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> 

在視圖要生成驗證碼的地方如下:

@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

+0

非常感謝您的幫助。我幾乎在那裏,但我的ContactUsController中的[CaptchaValidator]處理程序仍然收到錯誤。我確實有: 使用Recaptcha;使用MvcReCaptcha的 ; 但是,我還是收到此錯誤:(?是否缺少using指令或程序集引用) 錯誤類型或命名空間名稱「CaptchaValidatorAttribute」找不到 – 2myCharlie

+0

好吧,我得到的錯誤排序它出。顯然,我忘了將CaptchaValidatorAttribute.cs文件添加到Helpers文件夾中。無論如何,我的下一個問題是,基於教程,CreateComment(Int32 id,bool captchaValid)與我的ContactUs(ContactModels模型)函數是一樣的,對嗎?我只需要添加Int32 id,將bool captchaValid參數添加到函數中,是否正確? – 2myCharlie

+0

好的,而不是一個驗證碼圖像,我在ContactForm.cshtml視圖中獲得以下內容: – 2myCharlie

相關問題