我的角度应用程序由 ASP.NET webapi支持,我在其中提供索引.html并且angular从那里处理其他所有内容。我想使用捆绑,但我看不出我该怎么做。我是否必须使用剃须刀(或网络表单)来引用捆绑包?或者是否有一个选项可以为捆绑输出提供一个固定的名称,我可以在我的 src/hrefs 中引用?
澄清一下,我没有使用MVC或Webforms来提供html。您只需重定向到 index.html ,并且路由都是客户端的。我的捆绑包配置是使用 WebActivator.PostApplicationStartMethod 完成的。
首先,要只回答问题,您可以在 html 文件中使用纯链接
<script src='bundles/mybundle' type='text/javascript' language='text/javascript'></script>
=> 这会将您的 JavaScript 包包含在页面中
使用此方法时会遇到的问题是,如果您修改捆绑包中包含的 *.js 文件,则修改在捆绑包中将不可见。这都是关于"捆绑缓存破坏"的,这是 ASP.NET 的一个不错的功能,但只能从剃须刀模板中使用......显然,您还需要重新启动池(但这非常慢且难以自动化):)
若要解决此问题,可以使用以下命令定义自己的 ASP.NET MVC 控制器
using System.Linq;
using System.Web.Mvc;
using System.Web.Optimization;
namespace Controllers
{
public class DebugBundlesController : Controller
{
// GET: debugbundles/mybundle
public ActionResult Mybundle()
{
return InlineBundleJavaScript("~/bundles/mybundle");
}
private ActionResult InlineBundleJavaScript(string bundlePath)
{
//see https://blog.mariusschulz.com/2015/10/25/inlining-css-and-javascript-bundles-with-asp-net-mvc
var bundleContext = new BundleContext(HttpContext, BundleTable.Bundles, "~/bundles");
var bundle = BundleTable.Bundles.Single(b => b.Path == bundlePath);
var js = bundle.GenerateBundleResponse(bundleContext).Content;
return JavaScript(js);
}
}
}
你像这样使用它:
<script src='debugbundles/mybundle' type='text/javascript' language='text/javascript'></script>
=>现在,每次进行更改时,都会重新生成捆绑包。
请注意仅在开发期间使用它,因为您几乎消除了捆绑包的所有优点(即特别是客户端和服务器缓存)