经典ASP(或PHP等)中的ASP.NET MVC包

时间:2022-11-07 01:46:11

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.

我们有混合应用程序,它仍然运行ASP.NET MVC中的经典ASP应用程序的一部分。我想在经典ASP中也使用捆绑的javascript和样式。

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.

在ASP.NET中,我们可以很好地使用捆绑的东西和缓存,我们使用Script.Render,它将版本哈希添加到fetch url。

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

事实上,这种方法当然不适用于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.

我们可以直接从html

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.

问题是如果经典ASP没有给捆绑请求提供正确的哈希,MVC捆绑将返回头缓存:no-caching,这将指示浏览器不缓存它。

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.

你有什么好主意吗?可以在经典ASP中计算哈希吗?你能告诉捆绑在没有v = hash的情况下允许缓存吗?可以在启动时从MVC传输v = hash吗?我们有在Classic ASP和MVC之间传递变量的机制,但是这种哈希是从MVC启动代码可以达到的。

2 个解决方案

#1


3  

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

AardVark狂野的想法给了我一些想法,我自己想出来了。解决方案本身很简单。

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):

在ASP.NET MVC(Global.asax.cs或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).

这将生成文件dyn_legacy_bundle.inc,其中包含正确的

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

在经典ASP(或一些变态的PHP等):

<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.

然后,这将使用ASP.NET启动时生成的文件,并使用捆绑的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)

请注意,这也适用于任何其他框架(即PHP)

#2


1  

Another option:

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

设置处理程序(即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.

您可以使用查询字符串指定不同的包。

#1


3  

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

AardVark狂野的想法给了我一些想法,我自己想出来了。解决方案本身很简单。

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):

在ASP.NET MVC(Global.asax.cs或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).

这将生成文件dyn_legacy_bundle.inc,其中包含正确的

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

在经典ASP(或一些变态的PHP等):

<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.

然后,这将使用ASP.NET启动时生成的文件,并使用捆绑的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)

请注意,这也适用于任何其他框架(即PHP)

#2


1  

Another option:

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

设置处理程序(即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.

您可以使用查询字符串指定不同的包。