使用 ASP.NET MVC 的 ASP.NET CAPTCHA 控件示例出现问题
我正在尝试使用以下 A CAPTCHA Server Control for ASP.NET - 作者:Jeff ASP.NET MVC 站点中的 Atwood。提交表单时,自定义控件似乎没有验证。
有人使用 ASP.NET MVC 对此示例进行过任何工作吗?
我使用的基本代码如下:
<% using (Html.BeginForm()) { %>
<CaptchaControl:CaptchaControl ID="CaptchaControl" Name="Security" runat="server"></CaptchaControl:CaptchaControl>
<input type="submit" value="Send Message" />
<% } %>
I am trying to use the following A CAPTCHA Server Control for ASP.NET - by Jeff Atwood within an ASP.NET MVC site. The custom control doesn't seem to be validating when the form is submitted.
Is there anyone who has done any work with this sample using ASP.NET MVC?
The basic code I am using is as follows:
<% using (Html.BeginForm()) { %>
<CaptchaControl:CaptchaControl ID="CaptchaControl" Name="Security" runat="server"></CaptchaControl:CaptchaControl>
<input type="submit" value="Send Message" />
<% } %>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
确实不建议对 ASP.Net MVC 站点使用 ASP.Net 服务器控件。 ASP.Net MVC 不像常规 ASP.Net 那样执行页面生命周期。服务器控件可以呈现,但如果它们依赖于 ViewState 或 Postback 事件,则它们将不起作用。
查看验证码控件的代码,这似乎是问题所在。验证被编程为在 LoadPostBackData 期间发生。不幸的是,ASP.Net MVC 中没有回发的概念......因此没有回发数据。
It's not really recommended to use ASP.Net server controls for an ASP.Net MVC site. ASP.Net MVC does not execute the page lifecycle like regular ASP.Net does. The server controls may render, but if they rely on ViewState or Postback events, they will not work.
Looking at the code for the Captcha Control, this seems to be the problem. The validation is programmed to occur during LoadPostBackData. Unfortunately, there is no concept of a postback in ASP.Net MVC... and hence no postback data.
我写了一篇关于 ASP.NET MVC 验证码控件的完整博客文章。也许值得一看,因为它是最新的:
ASP.NET MVC 验证码控件
I've written a complete blog post about an ASP.NET MVC captcha control. Maybe it's worth to have a look at it, since it's very up to date:
ASP.NET MVC Captcha Control
更好的选择是简单地使用 NuGet 包库中的 reCaptchaMvcExtensions。这是一个易于安装和使用的软件包,使用完整的 google reCaptcha。
A better choice is to simply use the reCaptchaMvcExtensions from the NuGet package library. This is a simple to install and use package that uses the full google reCaptcha.