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