1

I have the following setup

public class TrackingTransform : IBundleTransform
{
    public void Process(BundleContext context, BundleResponse response)
    {
        const string apiTokenPlaceHolder = "{{ApiToken}}";
        var token = ConfigurationManager.AppSettings["Token"];

        response.Content = response.Content.Replace(apiTokenPlaceHolder, token);
    }
}

And add the transform to my bundle

var trackingBundle = new ScriptBundle("~/bundles/tracking/global").Include(
            "~/Scripts/mdb.mixpanel.js",
            "~/Scripts/mdb.tracking.global-handlers.js"
            );

        trackingBundle.Transforms.Add(new TrackingTransform());
        bundles.Add(trackingBundle);

Then I reference the bundle in the view like this

@Scripts.Render("~/bundles/tracking/global")

When debugging I can see that the {{ApiToken}} is replaced with the correct value in the response.Content but when I look at the script in the browser I still see the original placeholder.

I noticed that the bundles.Add(trackingBundle) is executed before the Process() method which is probably the reason for the problem. However I can't seem to find out why this is happening and how to fix it.

I was originally following this article even the Microsoft's docs suggest the same thing

Adrian Hristov
  • 1,957
  • 2
  • 16
  • 22
  • Updated the question with the way I reference the bundle – Adrian Hristov Sep 09 '15 at 11:09
  • Oops - just deleted my original comment by mistake! I would expect `bundles.Add` (when the bundle is created) to execute before `Process` (when it is requested). What you've written should definitely work. If you try to visit the bundle directly in the browser, and stick a breakpoint on the `Process` method, does it do the replacement then? Are you running the site in debug or release mode? – greg84 Sep 09 '15 at 11:12
  • I am currently running in debug and I can see the different javascript files in the browser instead of 1 big bundle not sure if that has anything to do with it – Adrian Hristov Sep 09 '15 at 12:41
  • Can you see if it works with bundling/minification on? ie - `BundleTable.EnableOptimizations = true;` added as the first line in your `RegisterBundles` method? – Jamie Dunstan Sep 09 '15 at 12:48
  • So that made the transformation work but is there a way to do that without bundling the files when debugging? – Adrian Hristov Sep 09 '15 at 12:55
  • @AdrianHristov - I guess there should be, but this question appears to suggest otherwise. http://stackoverflow.com/q/28632692/3130094. You can try the approach there though. – Jamie Dunstan Sep 09 '15 at 13:12
  • 1
    @AdrianHristov - This also looks promising. Some good info in this question. http://stackoverflow.com/a/26769287/3130094 – Jamie Dunstan Sep 09 '15 at 13:20

0 Answers0