如何在 ASP.NET MVC 中捕获 g-recaptcha-response?

Cas*_*ldi 4 data-binding asp.net-mvc recaptcha viewmodel

我正在尝试将带有 ReCaptcha 的表单发布到 mvc 控制器。

<div class="g-recaptcha" data-sitekey="some_site_key"></div>
Run Code Online (Sandbox Code Playgroud)

从chrome中的网络监视器我可以看到数据被发射

在此处输入图片说明

但是我正在努力在控制器中接收这些数据,我尝试使用视图模型 [Required(AllowEmptyStrings = false, ErrorMessage = "Captcha required")] public string googleCaptcha { get; set; }

但我无法将变量googleCaptcha映射 到 g-recaptcha-response,是否有允许我这样做的数据属性?

我显然不能命名变量g-recaptcha-response因为 c# 不允许这些命名约定

And*_*NET 5

有几个示例可以创建允许您指定别名的自定义 Bind 属性,如果您为多个属性设置别名,我会采用这种方式:https : //ole.michelsen.dk/blog/bind-a-model-属性到不同名称的查询字符串字段.html

但是,对于一次性方法,我只需读取提交的值并手动设置我的属性:

[HttpPost]
public ActionResult SubmitAction()
{
    var recaptchaResponse = Request["g-recaptcha-response"];

    var myCaptcha = new MyCaptcha();
    myCaptcha.googleCaptcha = recaptchaResponse

    //..
}
Run Code Online (Sandbox Code Playgroud)