3

まず、「変更」という用語は間違っている可能性があります。埋め込まれたリソースを実際に変更できるかどうかを尋ねるだけでオンラインに投稿している人が何人かいます。私がしたいのは、アセンブリ内のリソースを、ページに登録する前に検索して置換する一種のテンプレートとして使用することです-これは可能ですか?

例えば; アセンブリに埋め込みリソースとして数行の jQuery があり、このスクリプトでは、フロントエンド プログラマが設定できる CSS クラス名を参照しているとします。実装するまでCSSクラスがどうなるかわからないので、埋め込みリソースを調べて、たとえば$myclass$をThisClassNameに置き換える方法はありますか。

それが不可能な場合は、少なくとも教えてください。そうすれば、尻尾を追いかけるのをやめることができます。

4

2 に答える 2

1

HTTP ハンドラーを作成することで、小さな問題を解決しました。この例では、DynamicClientScript.axd と呼ばれます。

アイデアを提供するために、コードからいくつかのカットを取りました。以下のコードは、標準の埋め込みリソース URL を取得し、そこからクエリ文字列を取得して、ハンドラーへのパスに追加します。

    /// <summary>
    /// Gets the dynamic web resource URL to reference on the page.
    /// </summary>
    /// <param name="type">The type of the resource.</param>
    /// <param name="resourceName">Name of the resource.</param>
    /// <returns>Path to the web resource.</returns>
    public string GetScriptResourceUrl(Type type, string resourceName)
    {
        this.scriptResourceUrl = this.currentPage.ClientScript.GetWebResourceUrl(type, resourceName);

        string resourceQueryString = this.scriptResourceUrl.Substring(this.scriptResourceUrl.IndexOf("d="));

        DynamicScriptSessionManager sessMngr = new DynamicScriptSessionManager();
        Guid paramGuid = sessMngr.StoreScriptParameters(this.Parameters);

        return string.Format("/DynamicScriptResource.axd?{0}&paramGuid={1}", resourceQueryString, paramGuid.ToString());
    }

    /// <summary>
    /// Registers the client script include.
    /// </summary>
    /// <param name="key">The key of the client script include to register.</param>
    /// <param name="type">The type of the resource.</param>
    /// <param name="resourceName">Name of the resource.</param>
    public void RegisterClientScriptInclude(string key, Type type, string resourceName)
    {
        this.currentPage.ClientScript.RegisterClientScriptInclude(key, this.GetScriptResourceUrl(type, resourceName));
    }

次に、ハンドラーはクエリ文字列を取得して、標準リソースへの URL を構築します。リソースを読み取り、各キーをディクショナリ コレクション (DynamicClientScriptParameters) 内の値に置き換えます。

paramGuid は、正しいスクリプト パラメータ コレクションを取得するために使用される識別子です。

ハンドラーは何をしますか...

        public void ProcessRequest(HttpContext context)
    {
        string d = HttpContext.Current.Request.QueryString["d"]; 
        string t = HttpContext.Current.Request.QueryString["t"];
        string paramGuid = HttpContext.Current.Request.QueryString["paramGuid"];

        string urlFormatter = "http://" + HttpContext.Current.Request.Url.Host + "/WebResource.axd?d={0}&t={1)";

        // URL to resource.
        string url = string.Format(urlFormatter, d, t);

        string strResult = string.Empty;

        WebResponse objResponse;
        WebRequest objRequest = System.Net.HttpWebRequest.Create(url);

        objResponse = objRequest.GetResponse();

        using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()))
        {
            strResult = sr.ReadToEnd();

            // Close and clean up the StreamReader
            sr.Close();
        }

        DynamicScriptSessionManager sessionManager = (DynamicScriptSessionManager)HttpContext.Current.Application["DynamicScriptSessionManager"];

        DynamicClientScriptParameters parameters = null;

        foreach (var item in sessionManager)
        {
            Guid guid = new Guid(paramGuid);

            if (item.SessionID == guid)
            {
                parameters = item.DynamicScriptParameters;
            }
        }

        foreach (var item in parameters)
        {
            strResult = strResult.Replace("$" + item.Key + "$", item.Value);
        }

        // Display results to a webpage
        context.Response.Write(strResult);
    }

次に、リソースを参照するコードで、次を使用します。

            DynamicClientScript dcs = new DynamicClientScript(this.GetType(), "MyNamespace.MyScriptResource.js");

        dcs.Parameters.Add("myParam", "myValue");

        dcs.RegisterClientScriptInclude("scriptKey");

次に、スクリプト リソースに次のものが含まれているとします。

alert('$myParam$');

次のように出力されます。

alert('myValue');

私のコードは (DynamicScriptSessionManager を使用して) キャッシュも行いますが、アイデアはわかります...

乾杯

于 2010-03-24T09:31:11.000 に答える
0

コードビハインドでは、埋め込まれたリソースのコンテンツを読み取り、必要なものを切り替えて、新しいコンテンツを応答に書き込むことができます。このようなもの:

protected void Page_Load(object sender, EventArgs e)
{
    string contents = ReadEmbeddedResource("ClassLibrary1", "ClassLibrary1.TestJavaScript.js");
    //replace part of contents
    //write new contents to response
    Response.Write(String.Format("<script>{0}</script>", contents));
}

private string ReadEmbeddedResource(string assemblyName, string resouceName)
{
    var assembly = Assembly.Load(assemblyName);
    using (var stream = assembly.GetManifestResourceStream(resouceName))
    using(var reader = new StreamReader(stream))
    {
        return reader.ReadToEnd();
    }
}
于 2010-03-05T17:43:31.677 に答える