0

I have a Web Api based applicaiton. There is a method in an ApiController, which can be called as via Http-request, as via Hangfire handler.

The problem is I need to get a list of paths of files for a particular bundle, however I cannot find a right way how to do that. I have one solution if I have HttpContext:

   var httpContext = new HttpContextWrapper(HttpContext.Current);
   var reportScripts = registeredBundles.First((b) => b.Path == "~/bundles/report");

   BundleContext bundleContext = new BundleContext(httpContext, BundleTable.Bundles, "~/bundles/");
   var filePath = reportScripts.EnumerateFiles(bundleContext).First().IncludedVirtualPath;

However, if Hanfire calls my method I don't have HttpContext at all. So the approach above doesn't work. I've found a workaround here and here(just to mock HttpContext). But this solution looks very weird for me... and I would not use it.

Could anybody help me to find a way to get list of paths from a particular bundle? I was surprised why Bundle object has internal field "Items", but not a public one.

More details if my explanation was not clear enough: For example I have a bundle:

            bundles.Add(new ScriptBundle("~/bundles/report")
            .Include("~/Scripts/d3js/d3.min.js")
            .Include("~/Scripts/jquery/jquery-1.12.4.js"));

and I need to get the string "Scripts/d3js/d3.min.js" from there.

This is needed for me, because i render complicated report on the server side and send it to the customer. For the template of the report I need links for some scripts(i.e. Jquery, etc). I don't want to use any constants and hardcode paths to my Scripts, because I can change a version of a script or a file in future. In this case my report silently will be broken. In bundles I will always have up to date versions of the scripts, so if I could take paths from bundles it would be perfect!

Thanks in advance!

Sergey_T
  • 388
  • 6
  • 10

2 Answers2

1

The point of bundles is that the application is no longer serving individual files - it is serving bundles created from individual files. An effect of this is that from the client perspective this obscures which files went into the bundle.

That makes perfect sense, because if you serve the client a file with two functions, they don't care or need to know whether your source code stores those functions in one file or two. In a vague sense it would be like asking how many files of C# source code were used to compile the classes that were used to serve any given request.

The best suggestion I can give is to show the bundle URL on the report and provide some additional documentation to tell the customer what is in that bundle if they really want to know.

Scott Hannen
  • 21,450
  • 3
  • 33
  • 44
0
  1. Add

    using System.Web.Optimization;

  2. Enumerate included scripts using

    BundleResolver.Current.GetBundleContents(bundleUrl);

user1107799
  • 252
  • 2
  • 11