ASP.NET MVC bundles in Classic ASP (or PHP etc.)

不羁的心 提交于 2019-12-03 14:15:41

AardVark's wild thought gave me some ideas and I figured out it myself. Solution itself is quite simple.

Here is the solution for anyone who might need similar solution.

After you have registered the bundles in ASP.NET MVC (Global.asax.cs or BundleConfig):

        List<string> bundleHtml = new List<string>();
        bundleHtml.Add(Scripts.Render("~/bundles/legacybase").ToString());
        bundleHtml.Add(Styles.Render("~/styles/legacycss").ToString());
        File.WriteAllLines(Server.MapPath("~/dyn_legacy_bundle.inc"), bundleHtml, System.Text.Encoding.UTF8);

This will generate file dyn_legacy_bundle.inc that contains the proper <script>-tags that include the version hash (or debug versions if debug is enabled).

In Classic ASP (or some kinky PHP etc.):

<head>
   <!--#include file="dyn_legacy_bundle.inc" -->
</head>

This will then use the file that was generated on startup by ASP.NET, and use the bundled css/javascript.

Negative thing is that if bundled files are changed on runtime, this dynamic file is not updated. That will cause bundles not to be cached. App pool recycle will eventually fix caching, so I think we will live with it. Let me know if you figure out way to avoid this.

Notice that this will work with any other framework also (ie. PHP)

Another option:

Setup a handler (i.e. Bundles.ashx)

 public void ProcessRequest (HttpContext context) {
    context.Response.ContentType = "text/html";
    context.Response.Write(System.Web.Optimization.Styles.Render("~/css"));
}

From php:

echo file_get_contents("http://example.com/Bundles.ashx");

You could use the querystring to specify different bundles.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!