我現在可能完全偏離軌道,所以我只是在這裏問這個,所以有人可以幫助我。從web.config獲取價值applicationSettings到ASP.NET標記
我想要做的是從我的web.config中,將一個存儲在applicationSettings區域中的值插入到我的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, System, Version=2.0.0.0, Culture=neutral, 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。
編輯: 我相信我只能用ExpressionBuilder來做,因爲我必須將字符串與單個圖像名稱連接起來。我改變了上面的例子來反映這一點。
我喜歡伯特史密斯代碼解決方案下面的訪問配置部分,只有我需要把它放在表達式生成器。 我被困在覆蓋從我將調用配置管理器的GetCodeExpression方法,但我不明白如何構建表達式的參數。
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")%>'
,我只是用微軟的例子返回任何種類的來自表達式生成器的代碼:
返回新的CodeSnippetExpression(entry.Expression);
GetAppSetting是我的自定義Page類中的一種方法。
「我現在可能完全偏離了軌道」..我想說,如果您要將鏈接存儲到圖像資源在一個web.config文件中,那麼你是非常偏離軌道。 – NotMe 2011-05-20 15:34:07
經過反思,我*可能會讀這個錯誤。你只是存儲外部參考的開始部分或整個圖像位置?這導致下一個問題:這實際上是否支持多種環境(測試,階段,產品)? – NotMe 2011-05-20 15:36:44
只是URL的第一部分,所以我可以在不同的環境中更改 – 2011-05-23 10:36:50