从web.config applicationSettings获取ASP.NET标记的价值
我现在可能完全偏离轨道,所以我会在这里问这个,所以有人可以帮助我. 我想要做的是将存储在applicationSettings区域的web.config中的值插入到我的aspx标记中.具体来说,我想从配置中恢复URL.这是我使用的configSection设置 <configSections> <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup,System,Version=2.0.0.0,Culture=neutral,PublicKeyToken=123456"> <section name="MyApp.Properties.Settings" type="System.Configuration.ClientSettingsSection,PublicKeyToken=12345" requirePermission="false" /> </configSections> 稍后在该文件中的实际设置如下: <applicationSettings> <MyApp.Properties.Settings> <setting name="ImagesUrl" serializeAs="String"> <value>http://resources/images/</value> </setting> 现在我想在标记中引用上面的值,如下所示: <asp:Image ID="Image1" runat="server" ImageUrl="<%$AppSettings:ImagesUrl%>/Image1.jpg 我知道有一个表达式<%$AppSettings:ImagesUrl%>,但我没有使用web.config的appsettings部分,而是使用configSection. 编辑: 我喜欢下面的Bert Smith Code Solution来访问配置部分,只需要将它放在表达式构建器中. public class SettingsExpressionBuilder: ExpressionBuilder { public override CodeExpression GetCodeExpression(BoundPropertyEntry entry,object parsedData,ExpressionBuilderContext context) { return ?? } 编辑 <asp:ScriptReference Path='<%$Code:GetAppSetting("ResourcesUrl","JS/jquery/jquery.jqplot.js")%>' 我只是使用Microsoft的示例从表达式构建器返回任何类型的代码: 返回新的CodeSnippetExpression(entry.Expression); GetAppSetting是我自定义Page类中的一种方法. 解决方法通常,您将创建自定义设置类以读取这些值,如此 artical所述.就个人而言,我只会使用上面建议的appSettings,因为这是现有的功能,而你表面上的表现似乎足够了.但是,不了解您的情况,如果没有自定义设置,您尝试做的事情就可以解决: 在后面的代码中,我创建了一个受保护的函数来检索设置 protected string GetCustomSetting(string Section,string Setting) { var config = ConfigurationManager.GetSection(Section); if (config != null) return ((ClientSettingsSection)config).Settings.Get(Setting).Value.ValueXml.InnerText; return string.Empty; } 然后在aspx标记中我调用此函数 <div> <label runat="server" id="label"><%=GetCustomSetting("applicationSettings/MyApp.Properties.Settings","ImagesUrl") %></label> </div> 希望这可以帮助. 跟进: CodeExpression看起来像这样: public override CodeExpression GetCodeExpression(BoundPropertyEntry entry,ExpressionBuilderContext context) { var config = ConfigurationManager.GetSection("applicationSettings/MyApp.Properties.Settings"); return new CodePrimitiveExpression(((ClientSettingsSection)config).Settings.Get(entry.Expression).Value.ValueXml.InnerText); } 在我的测试中,我创建了一个名为CustomSettingsExpressionBuilder的类,并将其添加到App_Code文件夹中.将自定义快速的配置添加到web.config并从aspx中调用它,如下所示: <asp:Label ID="Label1" runat="server" Text="<%$CustomSettings:ImagesUrl %>"></asp:Label> (编辑:湘西站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |