9

私は今では完全に軌道から外れているかもしれないので、誰かが私を助けることができるようにここで質問します.

私がやりたいことは、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, 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でしかできないと思います。それを反映するために上記の例を変更しました。

config セクションにアクセスするための以下の Bert Smith Code Solution が気に入っていますが、それを式ビルダーに入れる必要があるだけです。構成マネージャーを呼び出す場所から 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")%>'

Microsoft の例を使用して、式ビルダーから任意の種類のコードを返しました。

新しい CodeSnippetExpression(entry.Expression) を返します。

GetAppSetting は、カスタム Page クラスのメソッドです。

4

3 に答える 3

11

通常、この記事で説明されているように、これらの値を読み取るカスタム設定クラスを作成します。個人的には、上記のように 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>
于 2011-05-20T19:44:35.843 に答える
1

マークアップする必要がありますか?コードビハインドで設定してみませんか。

Image1.ImageUrl= //fetch your settings here.

もう 1 つの方法は、コード ビハインドでプロパティまたは静的メソッドを定義し、それをマークアップで使用することです。

于 2011-05-20T15:51:50.807 に答える
0

ASP.NET ビットについてはよくわかりませんが、これが通常のコードである場合はMyApp.Properties.Settings.Default.ImagesUrl、試してみてください。

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

これを収納した方が断然楽<appSettings>です。

于 2011-05-20T15:44:54.653 に答える