1

私は.Net MVCのバンドルと縮小に取り組んでいます。次のような URL になることを除けば、問題なく機能します。

/bundles/AllMyScripts?v=r0sLDicvP58AIXN_mc3QdyVvVj5euZNzdsa2N1PKvb81

プロジェクトには AWS Cloudfront にある静的ファイルがあり、デフォルトではクエリ文字列は好きではありません。これをサポートするように変更できますが、パフォーマンスが低下します。

トークンをクエリ文字列ではなくファイル名に入れるようにバンドルを構成できますか? Web Grease 以外のものを使用することにもオープンです。

4

3 に答える 3

1

わかりました、URL の書き換えとカスタム html ヘルパーを含む適切なソリューションを思いつきました。

web.config:

  <rule name="BundlingRewrite" stopProcessing="true">
      <match url="^content/min/([^/]+)/([^/]+)/?$" />
      <conditions>
          <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
          <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
      </conditions>
      <action type="Rewrite" url="content/min/{R:1}?v={R:2}" />
  </rule>

ヘルパー:

public static IHtmlString RenderCdnCss(this HtmlHelper helper, params string[] paths)
{
    if (BundleTable.EnableOptimizations)
    {
        StringBuilder sb = new StringBuilder();
        Uri baseUri = helper.ViewContext.HttpContext.Request.Url;

        foreach (string s in paths) {
            Uri uri = new Uri(baseUri, BundleTable.Bundles.ResolveBundleUrl(s));
            sb.AppendFormat("<link href=\"{0}\" rel=\"stylesheet\"/>", uri.PathAndQuery.Replace("?v=", "/"));                    
        }
        return new HtmlString(sb.ToString());
    }
    return Styles.Render(paths);
}

ヘルパーは、バンドルされた URL をより CDN に適したものに変換します。例えば:

/content/min/css?v=3GWBEyScjC610oPQm0JVybboQ_EmX3StAuCZjd_B7bE1

になる

/コンテンツ/分/css/3GWBEyScjC610oPQm0JVybboQ_EmX3StAuCZjd_B7bE1

URL 書き換え (IIS Url Rewrite 2.0) は content/min/{some folder}/{some token} 内の URL を探し、それを content/min/{some folder}?v={some token} に書き換えます (パスは何ですか?デフォルトで見えます)

そのため、バンドラーはそれほど賢明ではなく、パスは CDN フレンドリーになります。私の場合、cdn の URL を URL の前に追加しますが、それは上記には含まれていません。

于 2013-05-15T19:00:30.947 に答える
0

MapRouteaと aを使用してController、バンドル URL を CDN フレンドリーに書き換えることができます。

/bundles/AllMyScripts?v=r0sLDicvP58AIXN_mc3QdyVvVj5euZNzdsa2N1PKvb81あなたの代わりに CDN_Bundle/bundles/AllMyScripts/r0sLDicvP58AIXN_mc3QdyVvVj5euZNzdsa2N1PKvb81.

路線図:

    routes.MapRoute(
            name: "CDN_Bundle",
            url: "CDN_Bundle/{*virtualPath}",
            defaults: new { controller = "CDN_Bundle", action = "Index" }

アクション:

    public ActionResult Index(string virtualPath)
    {
        virtualPath = virtualPath.Trim('/');
        int lastSlash = virtualPath.LastIndexOf("/");
        string hashCode = virtualPath.Substring(lastSlash + 1, virtualPath.Length - lastSlash -1 );
        virtualPath = virtualPath.Substring(0, virtualPath.LastIndexOf("/"));

        WebClient webClient = new WebClient();
        webClient.Headers.Add("user-agent", Request.UserAgent);
        Stream data = webClient.OpenRead(Request.Url.GetLeftPart(UriPartial.Authority) + Path.Combine(Request.ApplicationPath, virtualPath) + "?v=" + hashCode);
        StreamReader reader = new StreamReader(data);
        string content = reader.ReadToEnd();

        return Content(content);
    }

代わりにこれを使用しますScripts.Render

         <script src="/cdn_bundle@(System.Web.Optimization.BundleTable.Bundles.ResolveBundleUrl("~/bundles/AllMyScripts").Replace("?v=","/"))"></script>
于 2013-05-14T05:38:22.333 に答える