ReCaptcha and MVC3, problems getting Microsoft.Web

2019-04-14 07:01发布

问题:

I have reCaptcha installation problems. I already saw some posts about that, even in StackOverflow but it didn't help me to get it working.

I followed this post, and this post but in this line:

@using Microsoft.Web.Helpers

I get the message:

The type or namespace name 'Helpers' does not exist in the namespace 'Microsoft.Web' (are you missing an assembly reference?)

I added all references mentioned, all assemblies in both web.config files (root and view folder), restarted VS2010, updated the MVC3 package, included WebMatrix packages but I couldn't get it working.

I guess it should be simple to install, but I don't know what I'm doing wrong.

Anyone could help me?

回答1:

Here's a step by step guide:

  1. Create a new ASP.NET MVC 3 project using the default template
  2. Install the microsoft-web-helpers NuGet
  3. In the Index.cshtml view of HomeController create a form and bring the Microsoft.Web.Helpers namespace into scope:

    @using Microsoft.Web.Helpers
    
    @using (Html.BeginForm())
    {
        @ReCaptcha.GetHtml(publicKey: "__ put your public key here __")
        <button type="submit">OK</button>
    }
    
  4. And to validate the Captcha in the controller:

    [HttpPost]
    public ActionResult Index(MyViewModel model)
    {
        if (!ReCaptcha.Validate(privateKey: "__ put your private key here __"))
        {
            return View(model);
        }
        return RedirectToAction("success");
    }