2011-05-20 33 views
8

我現在可能完全偏離軌道,所以我只是在這裏問這個,所以有人可以幫助我。從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類中的一種方法。

+0

「我現在可能完全偏離了軌道」..我想說,如果您要將鏈接存儲到圖像資源在一個web.config文件中,那麼你是非常偏離軌道。 – NotMe 2011-05-20 15:34:07

+0

經過反思,我*可能會讀這個錯誤。你只是存儲外部參考的開始部分或整個圖像位置?這導致下一個問題:這實際上是否支持多種環境(測試,階段,產品)? – NotMe 2011-05-20 15:36:44

+0

只是URL的第一部分,所以我可以在不同的環境中更改 – 2011-05-23 10:36:50

回答

10

通常,您會創建一個自定義設置類來讀取這些值,如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, object parsedData, 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> 
+0

我喜歡這個想法,但我不能在服務器控件中使用自定義函數。 _「服務器標籤不能容器<%...%>構造」_ – 2011-05-23 10:41:31

+0

希望後續部分是你的需求。如果需要的話,我可以發佈整個班級,但看起來你正朝着正確的方向前進。 – 2011-05-23 12:51:41

+0

這不是我最後實施的,但它接近了。問題是,我仍然無法追加圖像名稱,除非我可以將exprBuilder中的「entry」字符串分開,或者有多個參數。所以我現在使用另一個表達式構建器,我已經從博客文章,編譯任何有效的C#代碼。我創建了一個返回所請求的appSetting字符串的方法,我可以只連接圖像名稱。 <%$ Code:GetAppSetting(「ResourcesUrl」)+「images/image.jpg」%> – 2011-05-24 08:54:27

0

我不知道關於它的ASP.NET位,但如果這是正常的代碼,你會怎麼做MyApp.Properties.Settings.Default.ImagesUrl,所以儘量

<asp:Image ID="Image1" runat="server" 
      ImageUrl="<%$MyApp.Properties.Settings.Default.ImagesUrl%> 

它肯定會更容易儲存這<appSettings>雖然。

1

它是否必須在標記?你爲什麼不在代碼隱藏中設置它。

Image1.ImageUrl= //fetch your settings here. 

彼此的方式,在您的代碼隱藏來定義一個屬性或靜態方法,然後使用該標記。