Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 815
Tuukka Lindroos Avatar asked May 06 '14 16:05

Tuukka Lindroos


People also ask

What are the two types of bundles in MVC 5?

You can see ScriptBundle and StyleBundle objects we are using for bundling the js and CSS types of files. ScriptBundle: is responsible for Script files i.e javascript (JS). StyleBundle: is responsible for stylesheet files i.e CSS.

Is Classic ASP compiled or interpreted?

ASP Classic is comprised of VBScript or JavaScript interpreted at run-time which means each page has a specific performance hit doe to line by line interpretation. The interpretation of the pages simply results in some inefficiency. ASP.NET however compiles the code the first time it is accessed.

What are bundles in ASP.NET MVC?

Bundling is a new feature in ASP.NET 4.5 that makes it easy to combine or bundle multiple files into a single file. You can create CSS, JavaScript and other bundles. Fewer files means fewer HTTP requests and that can improve first page load performance.

What is meant by classic asp?

Classic ASP is a server-side scripting environment that you can use to create and run dynamic web applications. With ASP, you can combine HTML pages, script commands, and COM components to create interactive web pages that are easy to develop and modify.


2 Answers

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)

like image 94
Tuukka Lindroos Avatar answered Sep 28 '22 03:09

Tuukka Lindroos


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.

like image 41
Duane Avatar answered Sep 28 '22 04:09

Duane