App_Code 中的 MVC Razor 声明式帮助程序不使用 web.config 命名空间
我在 App_Code 目录中使用声明性助手时遇到问题。 我为我的视图创建了一个基本的 webviewpage,其中包含我的本地化方法。 我还有一个枚举集合可供我的助手使用,它们围绕 CSS 名称。
例如,DataRole 是一个枚举,引用允许显示的 CSS 类,而 GetStr 是我的基类的一个方法,用于打印标签的本地化值:
@helper ReadOnlyColumns(DataRole role, string label, string contents)
{
<div class="@role">@GetStr(label)</div>
}
我的 web.config 具有基本页面类型的定义和正确的命名空间包括。这些设置适用于不在 App_Code 目录中的视图。
有谁知道如何使 App_Code 目录工作?我应该在里面放一个 web.config 吗?我不知所措。
我知道你可以做 @implements 和 @using,这很有效。但我正在努力为我们的软件奠定基础,并且认为没有必要包含我们编写的每个页面上都会使用的内容。
编辑,这是我在根目录和每个视图目录中的 web.config 设置:
<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="Company.Web.BaseWebViewPage">
<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="Company.Web" />
<add namespace="Company.Web.Enums" />
</namespaces>
</pages>
</system.web.webPages.razor>
谢谢,
Brad
I have an issue with using declarative helpers in the App_Code directory.
I created a base webviewpage for my views, which has my localization method in it.
I also have a collection of enums to use for my helpers, which revolve around CSS names.
For instance, DataRole is an enum referring to the allowed CSS classes for display and GetStr is a method of my base class to print the localization value of the label:
@helper ReadOnlyColumns(DataRole role, string label, string contents)
{
<div class="@role">@GetStr(label)</div>
}
My web.config has the definition for the base page type, and the correct namespaces to include. These settings work for views not in the App_Code directory.
Does anyone have any ideas how to make the App_Code directory work? Should I put a web.config inside it? I am at a loss.
I know you can do @implements and @using, and that works. But I am trying to build the foundation for our software and don't feel it should be necessary to include things that will be used on absolutely every page we write.
Edit, this is my web.config settings in the root, and every views directory:
<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="Company.Web.BaseWebViewPage">
<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="Company.Web" />
<add namespace="Company.Web.Enums" />
</namespaces>
</pages>
</system.web.webPages.razor>
Thanks,
Brad
App_Code
中的页面在WebRazorHostFactory
中硬编码 继承HelperPage
类。您无法更改该默认值。
Pages in
App_Code
are hard-coded inWebRazorHostFactory
to inherit theHelperPage
class.You cannot change that default.