未找到 Razor HtmlHelper 扩展(或其他视图命名空间) [英] Razor HtmlHelper Extensions (or other namespaces for views) Not Found

查看:28
本文介绍了未找到 Razor HtmlHelper 扩展(或其他视图命名空间)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道这是在 PR 还是 Beta 中发生的,但是如果我在 HtmlHelper 上创建扩展方法,它在 Razor 驱动的页面中无法识别:

I don't know if this was happening in the PR or Beta, but if I create an extension method on HtmlHelper, it is not recognized in a Razor powered page:

namespace SomeNamespace.Extensions {
    public static class HtmlExtensions {
        public static string Foo(this HtmlHelper html) {
            return "Foo";
        }
    }
}

我将其添加到 Web.config 中的 部分:

I added it to the <Namespaces> section in Web.config:

<pages>
  <namespaces>
    <add namespace="System.Web.Mvc" />
    <!-- snip -->
    <add namespace="SomeNamespace.Extensions"/>
  </namespaces>
</pages>

但是在尝试查看页面时会抛出编译错误:

But it throws a compile error when trying to view the page:

@Html.Foo()

如果我使用 WebForms 重新创建页面,它就可以正常工作.怎么回事?

If I recreate the page with WebForms it works fine. What's the deal?

解决方法

如果我在 Razor 视图中包含 @using SomeNamespace.Extensions,那么它可以工作,但我更愿意将它放在 Web.config

If I include @using SomeNamespace.Extensions in my Razor view, then it works, but I'd much rather just have it in Web.config

推荐答案

从 Beta 版开始,Razor 使用不同的配置部分来全局定义命名空间导入.在您的 ViewsWeb.config 文件中,您应该添加以下内容:

Since the Beta, Razor uses a different config section for globally defining namespace imports. In your ViewsWeb.config file you should add the following:

<configSections>
  <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
    <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
    <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
  </sectionGroup>
</configSections>

<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" />
      <!-- Your namespace here -->
    </namespaces>
  </pages>
</system.web.webPages.razor>

使用 MVC 3 升级工具 自动确保您拥有正确的配置值.

Use the MVC 3 upgrade tool to automatically ensure you have the right config values.

注意,您可能需要关闭并重新打开文件才能让编辑器获取更改.

Note that you might need to close and reopen the file for the changes to be picked up by the editor.

这篇关于未找到 Razor HtmlHelper 扩展(或其他视图命名空间)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆