ASP.NET捆绑在普通的HTML? [英] ASP.NET Bundles in regular html?
问题描述
我的角度应用程序由ASP.NET的WebAPI,在这里我提供了支持一个index.html和角度处理从那里一切。我想用捆绑销售,但我不看我怎么会做这个。我必须用剃刀(或WebForms的)只是引用包?还是有给束输出我可以在我的src /的HREF引用固定名称的选项?
My angular application is backed by ASP.NET webapi, where I'm serving up an index.html and angular handles everything else from there. I'd like to use bundling, but I can't see how I'd do this. Do I have to use razor (or webforms) just to reference bundles? Or is there an option to give the bundle output a fixed name that I can reference in my src/hrefs?
要澄清一下,我没有使用MVC或Web表单处理HTML。你只是重定向到的index.html,并且路由是所有客户端。我的包配置是使用WebActivator.PostApplicationStartMethod完成。
To clarify, I'm not using MVC or Webforms to serve html. You just get redirected to index.html , and the routing is all client-side. My bundle configuration is done using WebActivator.PostApplicationStartMethod.
推荐答案
首先,只回答这个问题,你可以使用普通的链接在HTML文件
First, to only answer the question, you can just use a plain link in your html file
<script src='bundles/mybundle' type='text/javascript' language='text/javascript'></script>
=>将包括你的JavaScript捆绑到页面
=> that will include your javascript bundle into the page
你将有这种方法的问题是,如果修改包含在包中的* js文件,修改不会在束是可见的。
这是所有关于包缓存无效,ASP.NET的一个很好的功能,但是从剃刀模板只可用...
很明显,你也CAL重新启动池(不过这是很慢,难以自动):)
The problem you will have with this approach is that if you modify a *.js file contained in the bundle, the modification will not be visible in the bundle. This is all about "bundle cache busting", a nice feature of ASP.NET but only usable from a razor template ... Obviously, you cal also restart the pool (but this is quite slow and hard to automate) :)
要解决这个问题,你可以用下面的定义自己的ASP.NET MVC控制器
To workaround the problem, you can define your own ASP.NET MVC Controller with the following
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>
=>现在,每次你做出改变的时候,包将会重新生成。
=> now, every time you make a change, the bundle will be regenerated.
请注意,因为你删除软件包的几乎所有优点只在开发过程中使用它(即特别是客户端和服务器缓存)
Be aware to use this only during development because you remove nearly all benefits of bundles (i.e in particular client and server caching)
这篇关于ASP.NET捆绑在普通的HTML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!