9

This is what my Html helper looks like:

namespace WebApp.WebUI
{
    public static class HtmlExtensions
    {

            public static MvcHtmlString GenerateCaptcha(this HtmlHelper helper, string theme)
            {
                string publicKey = ConfigurationManager.AppSettings["CaptchaKey_Public"];
                string privateKey = ConfigurationManager.AppSettings["CaptchaKey_Private"];
                var captchaControl = new Recaptcha.RecaptchaControl
                        {
                            ID = "recaptcha",
                            Theme = theme,
                            PublicKey = publicKey,
                            PrivateKey = privateKey
                        };

                var htmlWriter = new HtmlTextWriter(new StringWriter());

                captchaControl.RenderControl(htmlWriter);

                return new MvcHtmlString(htmlWriter.InnerWriter.ToString());
            }

     }
}

I tried using it in this view:

    @{
        ViewBag.Title = "Register";
    }
    @model WebApp.WebUI.ViewModel.RegisterModel

    @using (Html.BeginForm("Register", "Auth", FormMethod.Post, new { Id = "ERForm" }))
    {
        @Html.GenerateCaptcha("clean")   
    }

It gives me this error:

CS1061: 'System.Web.Mvc.HtmlHelper<WebApp.WebUI.ViewModel.RegisterModel>' does not contain a definition for 'GenerateCaptcha' and no extension method 'GenerateCaptcha' accepting a first argument of type 'System.Web.Mvc.HtmlHelper<WebApp.WebUI.ViewModel.RegisterModel>' could be found (are you missing a using directive or an assembly reference?)

What am I doing wrong. My namespaces are correct. It does not show up in the intellisense for @Html

4 Answers 4

17

You could add:

@using WebApp.WebUI

on the top of your Razor view.

And if you want to reuse this helper among many different views to avoid adding the using clause everytime you could add it to the <namespaces> section of the ~/Views/web.config file:

<system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
        <namespaces>
            <add namespace="System.Web.Mvc" />
            <add namespace="System.Web.Mvc.Ajax" />
            <add namespace="System.Web.Mvc.Html" />
            <add namespace="System.Web.Routing" />
            <add namespace="WebApp.WebUI" />
        </namespaces>
    </pages>
</system.web.webPages.razor>

After doing this make sure you recompile and reopen the Razor view for the Intellisense to have time to pick it up.

Sign up to request clarification or add additional context in comments.

2 Comments

I'm supposed to be able to add the namespace in the <pages><namespaces> and not use @using WebApp.WebUI right?
Did you every have any luck getting this to work without the using directive locally in the razor view? That works for me but but I still can't get it to work globally (without the @using and just the @Html.<TheHelper>).
1

Like Darin said but, in order to use it globally, you probably need to add it to both ~/Views/web.config and ~/web.config section.

Comments

1

To make html helpers work globally, follow Darin Dimitrov's answer. Once that's done, close the .cshtml view and reopen it, the intellisense starts working.

Reference: Razor - mvc 3 - Namespace to web.config works but intellisense does not recognize extension

Comments

0

For me I was using Nopcommerce and had to add the using statement @using Nop.Web.Framework.Security.Captcha

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.