ASP.NET MVC 5.0 Basic CAPTCHA VB.NET Code Example

The ASP.NET MVC 5.0 Basic Captcha Simple API VB.NET example project shows the most basic source code required to protect an ASP.NET MVC form with BotDetect CAPTCHA and validate the user input.

First Time Here?

Check the BotDetect ASP.NET MVC Captcha Simple API Quickstart for key integration steps.

ASP.NET MVC View code displaying CAPTCHA protection can be found in Views/Example/Index.vbhtml, and the ASP.NET MVC Controller code checking user input is in Controllers/ExampleController.vb.

Download the BotDetect ASP.NET CAPTCHA Component and run this example

→ ASP.NET MVC version:

→ .NET programming language:

  • C#
  • VB.NET

Installed Location

By default, the .NET 4.5 VB.NET version of the ASP.NET MVC 5.0 Basic Captcha Simple API example project is installed at:
C:\Program Files\Captcha Inc\BotDetect 4 CAPTCHA Component\Asp.Net\.NET\WebApp\SimpleAPI\Mvc50BasicCaptchaExample\VBNet

You can also run it from the BotDetect Start Menu:
Programs > Captcha Inc > BotDetect 4 CAPTCHA Component > ASP.NET > ASP.NET Examples > Run .NET Examples


@* namespaces needed to access BotDetect members *@
@Imports BotDetect.Web.Mvc
  Layout = Nothing
End Code
<!DOCTYPE html>

  <meta name="viewport" content="width=device-width" />
  <title>BotDetect ASP.NET CAPTCHA Validation: Basic ASP.NET MVC CAPTCHA Code 
  <link href="@Url.Content("~/Style/Sheet.css")" rel="stylesheet" 
  type="text/css" />
  <div class="column">
    <h1>BotDetect ASP.NET CAPTCHA Validation: <br /> Basic ASP.NET MVC CAPTCHA 
    Code Example</h1>
    @Using Html.BeginForm()
        <legend>ASP.NET MVC CAPTCHA Validation</legend>
          @Html.Label("CaptchaCode", "Retype the code from the picture:")
          @Code Dim exampleCaptcha As MvcSimpleCaptcha = New MvcSimpleCaptcha("ExampleCaptcha") End Code
        <div class="actions">
          <input type="submit" value="Validate" />
          @If ((Request.HttpMethod = "POST") And ViewData.ModelState.IsValid) 
            @<span class="correct">Correct!</span>
          End If
    End Using

To display Captcha protection on the example View, we first ensure we can use BotDetect members by Import-ing the relevant namespaces.

We then create a MvcSimpleCaptcha instance, and add it to the form by calling the Html.SimpleCaptcha() Html helper with it.

In this simplest case, we also use the Html.ValidationMessage helper to display Captcha validation errors.


<?xml version="1.0" encoding="UTF-8"?>
<botdetect xmlns="" 



In botdetect.xml, we configure captcha options for the ExampleCaptcha captcha style name. You can find a full list of available Captcha configuration options and related instructions at the Captcha configuration options page .


Imports BotDetect.Web.Mvc
Imports System.Web.Mvc

Namespace AspNetMvc50BasicCaptchaExampleVBNet
  Public Class ExampleController
    Inherits System.Web.Mvc.Controller

    ' GET: /Example

    Function Index() As ActionResult
      Return View()
    End Function

    ' POST: /Example/

    <HttpPost()> _
    <SimpleCaptchaValidation("CaptchaCode", "ExampleCaptcha", "Incorrect!")> _
    Public Function Index(ByVal model As ExampleModel) As ActionResult

      Return View(model)
    End Function

  End Class
End Namespace

After we've included the BotDetect.Web.Mvc namespace, we just need to add the SimpleCaptchaValidation attribute to the method processing form submissions. The attribute takes three parameters:

  1. the ID of the textbox containing the user's Captcha code input (which we named CaptchaCode on the form),
  2. the ID of the SimpleCaptcha instance we're validating (which we set to ExampleCaptcha in the MvcSimpleCaptcha constructor), and
  3. the error message to be shown when SimpleCaptcha validation fails.

When the SimpleCaptcha validation action filter attribute has been added, the SimpleCaptcha validation will trigger every time the form is submitted, and will automatically add a Model error with the error message configured above when SimpleCaptcha validation fails. The Html.ValidationMessage helper on the form will then display this error when SimpleCaptcha validation fails.


Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.Mvc
Imports System.Web.Routing

Public Module RouteConfig
  Public Sub RegisterRoutes(ByVal routes As RouteCollection)

    ' BotDetect requests must not be routed
    New With {.botdetect = "(.*)BotDetectCaptcha\.ashx"})

    routes.MapRoute( _
        name:="Default", _
        url:="{controller}/{action}/{id}", _
        defaults:=New With {.controller = "Example", .action = "Index", .
        id = UrlParameter.Optional} _
  End Sub
End Module

We configure ASP.NET Routing to ignore BotDetect requests, since they do not conform to any MVC-related patterns. The regex defining requests to ignore must match the path configured for the BotDetect HttpHandler registered in web.config.


<?xml version="1.0" encoding="utf-8"?>
  For more information on how to configure your ASP.NET application, please 
  <add key="webpages:Version" value=""/>
  <add key="webpages:Enabled" value="false"/>
  <add key="ClientValidationEnabled" value="false"/>
  <add key="UnobtrusiveJavaScriptEnabled" value="false"/>
  <compilation debug="false" targetFramework="4.5.1"/>
  <httpRuntime targetFramework="4.5.1"/>
    <!-- register HttpHandler used for BotDetect Simple API requests -->
    <add verb="GET" path="BotDetectCaptcha.ashx" 
    type="BotDetect.Web.SimpleCaptchaHandler, BotDetect"/>
  <validation validateIntegratedModeConfiguration="false"/>
    <!-- register HttpHandler used for BotDetect Simple API requests -->
    <remove name="BotDetectCaptchaHandler"/>
    <add name="BotDetectCaptchaHandler" preCondition="integratedMode" verb="GET" 
    path="BotDetectCaptcha.ashx" type="BotDetect.Web.SimpleCaptchaHandler, BotDetect"/>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <assemblyIdentity name="System.Web.Helpers" 
    <bindingRedirect oldVersion="" newVersion=""/>
    <assemblyIdentity name="System.Web.WebPages" 
    <bindingRedirect oldVersion="" newVersion=""/>
    <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35"/>
    <bindingRedirect oldVersion="" newVersion=""/>

To allow the application to use BotDetect Captcha protection, we must register the BotDetect HttpHandler in both <system.web><httpHandlers> and <system.webServer><handlers> configuration sections.

The <dependentAssembly> entry for System.Web.Mvc is also needed to make all ASP.NET MVC dependencies referenced by the BotDetect MVC assembly point to the correct ASP.NET MVC version.