1

I am currently using the gulp-bundle-assets module in a task to bundle css and js in my project. Each time I run it the module generates new filename making sure the browser will pick the latest bundle. However I need to change my file references manually in my html whenever the filename changes. The gulp-bundle-asset suggests a way to programmatically update the views by reading a json file.

What would be the proper way to handle the bundling with dynamic filenames in Visual Studio?

How are the relative paths for static content treated such as images,fonts?

Thanks!

3
  • 1
    I use gulp-usemin which does this automatically.. you can have a look at that Commented May 18, 2015 at 3:22
  • 1
    else you can use gulp-inject to inject the newly created files in index.html Commented May 18, 2015 at 3:23
  • 1
    Are you required to use gulp? As of ASP.NET 4.5 bundling and minification are now supported out of the box. Commented May 18, 2015 at 14:36

1 Answer 1

1

I'm the author of gulp-bundle-assets. I actually created an example of this awhile back using ASP.NET 5 (now named ASP.NET Core v1) seen here: https://github.com/dowjones/gulp-bundle-assets-example-aspnet-5. You'll want to rely on the bundle.result.json file.

The key pieces are as follows:

// 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));
}

And in your view:

@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>
    ...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.