ASP.Net 5 js,css捆绑

时间:2015-05-17 16:34:03

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

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

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

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

谢谢!

1 个答案:

答案 0 :(得分: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>
    ...