6
votes

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.

2
Hashing , Bundling , MVC ? In [classic-asp] ? Are you absolutely and totally sure that you are talking about Classic ASP ? - Rafael
We have hybrid application that uses both ASP.NET MVC4 and Classic ASP, while we slowly migrate to ASP.NET MVC4. Hybrid stuff works quite nicely, because http, html and javascript works in both. For example classic ASP page can do jquery ajax calls to ASP.NET MVC controller and get pure HTML outputs. - Tuukka Lindroos
Trust me, I know what I am doing. -- Sledgehammer. Please reconsider the negative vote, this is legitimate question. - Tuukka Lindroos
Just a wild thought. Can't you just work with a (classic-asp) include file that calls a (.NET mvc) view with only the bundled scripts? - AardVark71
Hmm, do you think adding <script> block dynamically, or MVC view that actually contains the bundled scripts? Either of them might actually work. - Tuukka Lindroos

2 Answers

3
votes

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)

1
votes

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.