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

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

问题描述

说不上来,如果这是发生在PR还是Beta版,但如果我创建的HtmlHelper 扩展方法,它不是在剃刀供电页公认的:

Dunno 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";
        }
    }
}

我把它添加到<&命名空间GT; 的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?

解决方法

如果我包括 @using SomeNamespace.Extensions 在我的Razor视图,那么它的工作原理,但我宁愿只是有它在网​​络的.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

推荐答案

公测以来,剃刀使用不同的配置部分定义全局命名空间的进口。在你的视图\\ Web.config文件文件,你应该增加以下内容:

Since the Beta, Razor uses a different config section for globally defining namespace imports. In your Views\Web.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.

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

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