【问题标题】:Problem recognizing html helpers in asp.net mvc 3 razor在 asp.net mvc 3 razor 中识别 html 助手的问题
【发布时间】:2011-04-27 18:33:20
【问题描述】:

这就是我的 Html 助手的样子:

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());
            }

     }
}

我尝试在这个视图中使用它:

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

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

它给了我这个错误:

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?)

我做错了什么。我的命名空间是正确的。它没有出现在 @Html 的智能感知中

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-3


    【解决方案1】:

    您可以添加:

    @using WebApp.WebUI
    

    在 Razor 视图的顶部。

    如果您想在许多不同的视图中重用此帮助程序以避免每次都添加 using 子句,您可以将其添加到 ~/Views/web.config 文件的 <namespaces> 部分:

    <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>
    

    完成此操作后,请确保重新编译并重新打开 Razor 视图,以便 Intellisense 有时间将其拾取。

    【讨论】:

    • 我应该能够在 中添加命名空间而不使用 @using WebApp.WebUI 对吗?
    • 在 razor 视图中没有本地 using 指令的情况下,你们每个人都幸运地让它工作吗?这对我有用,但我仍然无法让它在全球范围内工作(没有@using 和只有@Html.)。
    【解决方案2】:

    就像 Darin 说的,但是为了在全局范围内使用它,您可能需要将它添加到 ~/Views/web.config 和 ~/web.config 部分。

    【讨论】:

      【解决方案3】:

      要使 html 助手在全球范围内工作,请遵循 Darin Dimitrov 的回答。完成后,关闭 .cshtml 视图并重新打开它,智能感知开始工作。

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

      【讨论】:

        【解决方案4】:

        对我来说,我使用的是 Nopcommerce,必须添加 using 语句 @using Nop.Web.Framework.Security.Captcha

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-09-04
          • 2011-08-04
          • 1970-01-01
          • 2011-10-30
          • 2011-06-12
          相关资源
          最近更新 更多