【问题标题】:ASP.Net 5 js, css bundlingASP.Net 5 js、css 捆绑
【发布时间】:2016-02-13 03:19:25
【问题描述】:

我目前在一个任务中使用gulp-bundle-assets 模块在我的项目中捆绑css和js。每次我运行它时,模块都会生成新的文件名,确保浏览器会选择最新的包。但是,每当文件名更改时,我都需要在我的 html 中手动更改我的文件引用。 gulp-bundle-asset 提出了一种通过读取 json 文件以编程方式更新视图的方法。

在 Visual Studio 中处理与动态文件名捆绑的正确方法是什么?

图像、字体等静态内容的相对路径如何处理?

谢谢!

【问题讨论】:

  • 我使用 gulp-usemin 自动执行此操作.. 你可以看看
  • 否则你可以使用 gulp-inject 在 index.html 中注入新创建的文件
  • 你需要使用 gulp 吗?从 ASP.NET 4.5 开始,现在支持捆绑和缩小 out of the box

标签: asp.net-mvc-5 gulp web-essentials


【解决方案1】:

我是gulp-bundle-assets 的作者。实际上,我不久前使用 ASP.NET 5(现在命名为 ASP.NET Core v1)创建了一个示例:https://github.com/dowjones/gulp-bundle-assets-example-aspnet-5。您需要依赖 bundle.result.json 文件。

关键部分如下:

// read bundle.result.json
public async Task<dynamic> GetBundleResult()
{
    string fileName = Path.Combine(_appEnvironment.ApplicationBasePath, "bundle.result.json");
    string jsonText = File.ReadAllText(fileName);
    return await Task.FromResult(JObject.Parse(jsonText));
}

在你看来:

@inject ExampleMVC6Application.Services.BundlerService Bundler
@{
    dynamic Bundles = await Bundler.GetBundleResult();
}
<!DOCTYPE html>
<html>
    <head>
        @Html.Raw((object)Bundles.vendor.styles)
        @Html.Raw((object)Bundles.main.styles)
    </head>
    ...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-05
    • 1970-01-01
    • 2014-09-25
    • 2012-03-23
    • 2018-12-25
    • 1970-01-01
    • 2021-07-23
    相关资源
    最近更新 更多