如何向 Razor 页面添加额外的命名空间而不是 @using 声明? [英] How to add extra namespaces to Razor pages instead of @using declaration?

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

问题描述

是否需要使用某种魔法来获取 webconfig 中 pages/namespaces 元素中的命名空间?

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 的答案:未找到 Razor HtmlHelper 扩展(或其他用于视图的命名空间)

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

这在 MVC 3 Preview 1 和 MVC 3 Beta(今天刚刚发布)之间发生了变化.在预览版 1 中,Razor 使用了 WebForms 命名空间配置部分.然而,在 Beta 版中有一个新的配置部分,与 WebForms 分开.您需要将以下内容添加到您的 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.

请注意,为了让 Razor 在 MVC3 Beta 中工作,web.config 中需要的内容还有其他更改,因此您最好查看 ~ViewWeb.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 ~ViewWeb.config file that ships in the Beta project templates.

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

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