首先,只回答问题,您可以在 html 文件中使用纯链接
<script src='bundles/mybundle' type='text/javascript' language='text/javascript'></script>
=> 这会将您的 javascript 包包含到页面中
使用这种方法会遇到的问题是,如果您修改包中包含的 *.js 文件,则该修改将在包中不可见。这都是关于“捆绑缓存破坏”的,这是 ASP.NET 的一个不错的功能,但只能从 razor 模板中使用......显然,您还可以重新启动池(但这非常慢且难以自动化):)
要解决此问题,您可以使用以下内容定义自己的 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>
=> 现在,每次您进行更改时,都会重新生成捆绑包。
请注意仅在开发期间使用它,因为您几乎消除了捆绑包的所有好处(即特别是客户端和服务器缓存)