Is there a way to programmatically render a script bundle?
Using script bundles in MVC is great. Like it. Use it.
Now I'm writing an application that uses Self-Host WebApi in which I'd like to also create a script bundle. That is, I have a number of javascript files in the project that I wish to minify, combine and then return upon request.
I guess this should be possible. Can't seem to find any info on it, though.
Unfortunately the ASP.NET bundling mechanism is tightly coupled with an ASP.NET context. Using it in a self host would require some additional work. For example you will need to have a custom virtual path provider which will be able to read files from a specified location:
then you could have a bundle.config file attached to your console application where you would register all your bundle files:
next you could write your self-host where you would replace the default virtual path provider with the custom one we have just written:
and finally you could have some API controller which will serve the custom bundle that we defined in our
bundle.config
file:In this example I have omitted the client side caching that you should obviously take into account inside the controller in order to serve the bundled content with the corresponding
Cache
headers.