【问题标题】:ASP.NET Bundles in regular html?常规 html 中的 ASP.NET 捆绑包?
【发布时间】:2014-03-02 05:14:13
【问题描述】:

我的 Angular 应用程序由 ASP.NET webapi 提供支持,我在其中提供一个 index.html,而 Angular 则从那里处理所有其他内容。我想使用捆绑,但我不知道该怎么做。我必须使用剃刀(或网络表单)来引用捆绑包吗?或者是否有一个选项可以给捆绑输出一个固定的名称,我可以在我的 src/hrefs 中引用它?

为了澄清,我没有使用 MVC 或 Webforms 来提供 html。你只是被重定向到 index.html ,并且路由都是客户端的。我的包配置是使用 WebActivator.PostApplicationStartMethod 完成的。

【问题讨论】:

  • 所以您使用的是纯 html(没有 asp.net-mvc)并从 web api 获取数据?
  • @shenku - 在原帖中澄清
  • @GeorgeR 你找到合适的解决方案了吗?我处于同样的情况,我无法处理包刷新(即,当我在 js 文件中进行更改并重建项目时,包没有刷新)

标签: asp.net web-optimization


【解决方案1】:

首先,只回答问题,您可以在 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>

=> 现在,每次您进行更改时,都会重新生成捆绑包。

请注意仅在开发期间使用它,因为您几乎消除了捆绑包的所有好处(即特别是客户端和服务器缓存)

【讨论】:

    猜你喜欢
    • 2017-11-20
    • 2013-09-24
    • 1970-01-01
    • 2017-02-05
    • 2021-11-16
    • 1970-01-01
    • 1970-01-01
    • 2010-10-15
    • 1970-01-01
    相关资源
    最近更新 更多