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

萝らか妹 提交于 2019-12-05 00:11:34

问题


We have hybrid application that is still running part of the application with classic ASP along ASP.NET MVC. I would like to use bundled javascript and styles in classic ASP also.

In ASP.NET we can nicely use bundled stuff with caching nicely, we are using Script.Render, which adds version hash to the fetch url.

Thing is that this method of course isn't available in Classic ASP.

We can use bundled directly from html <script src="bundles/js?v=<%=version%>"/>. version variable is classic ASP variable used in cache busting (force browser refresh). It is changed between versions.

Problem is that if classic ASP doesn't give right hash to bundle request, MVC bundling will return header caching: no-caching, which will indicate to browser to not cache it.

Do you have any good ideas? Could that hash be computed in classic ASP? Could you tell bundling to allow caching without v=hash? Could v=hash be transferred from MVC in startup? We have mechanisms to transfer variables between Classic ASP and MVC, but is that hash some way reachable from MVC startup code.


回答1:


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)




回答2:


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.



来源:https://stackoverflow.com/questions/23500058/asp-net-mvc-bundles-in-classic-asp-or-php-etc

工具导航Map