如何额外的命名空间添加到页面剃刀,而不是@using申报? [英] How to add extra namespaces to Razor pages instead of @using declaration?

查看:177
本文介绍了如何额外的命名空间添加到页面剃刀,而不是@using申报?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有某种魔力,我需要使用获得的页/命名空间元素的命名空间中的webconfig?

Is there some sort of magic I need to use to get the namespaces in the pages/namespaces element in the webconfig?

<pages>
    <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="System.Web.WebPages"/>
        <add namespace="System.Web.Helpers"/>
        <add namespace="MyCustomHelpers"/>
    </namespaces>
</pages>

以上只是的的想工作。我知道这个命名空间是很好,因为当我把 @using MyCustomHelpers 它神奇的工作页面的顶部。

The above just doesn't want to work. I know the namespace is fine because when I put the @using MyCustomHelpers at the top of the page it magically works.

这是为了让我能获得 Html.SomeFunction(),而不必把工作 @using 在我所有页面的顶部

This is so that I can get the Html.SomeFunction() to work without having to put @using at the top of all my pages

推荐答案

更新:请在我的答案更新适用于MVC 3 RC看看:<一href=\"http://stackoverflow.com/questions/4136703/razor-mvc-3-rc-htmlhelper-extensions-not-found/4136773#4136773\">Razor (MVC 3 RC)的HtmlHelper扩展未找到

Update: please take a look at my updated answer that applies to MVC 3 RC: Razor (MVC 3 RC) HtmlHelper Extensions Not Found

这已经MVC 3 preVIEW 1和MVC 3测试版(只是今天公布)之间变化。在preVIEW 1剃须刀使用的WebForms命名空间配置部分。然而,在测试有一个新的配置部分,它是从的WebForms 1独立。您需要的follwing添加到您的web.config文件(或只是从模板一个全新的项目启动):

This has changed between MVC 3 Preview 1 and MVC 3 Beta (released just today). In Preview 1 Razor used the WebForms namespaces config section. However in the Beta there is a new config section that is seperate from the WebForms one. You will need to add the follwing to your web.config file (or just start with a brand new project from the template):

<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>
  <pages pageBaseType="System.Web.Mvc.WebViewPage">
    <namespaces>
      <add namespace="MyCustomHelpers" />
    </namespaces>
  </pages>
</system.web.webPages.razor>

注意,您可能需要关闭并重新打开了由编辑器拿起变更的文件。

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

请注意,有其他变化的是,在web.config中需要得到剃刀在MVC3测试工作,所以你最好是去看看在〜\\查看\\ Web.config文件文件附带的测试项目模板。

Note that there are other changes to what is required in web.config to get Razor to work in MVC3 Beta so you would be best off to take a look at the ~\View\Web.config file that ships in the Beta project templates.

这篇关于如何额外的命名空间添加到页面剃刀,而不是@using申报?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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