@Blog.Author(Nandip Makwana) .LearningExperience(ASP.NET, ASP.NET MVC, IIS, jQuery & Technology Surrounding it...)

November 11, 2012 comment , , , , ,

ASP.NET 4.5 & MVC 4 Bundling: Next Approach

In the earlier post, we have seen how we can access bundle context while processing bundle through custom transform. Taking it further, in this post we will see how we can utilize bundle context to pass extra parameters to transform and later how we can utilize those parameters to transform bundle.

What are the needs of extra parameter?

Since ASP.NET 4.5 & MVC 4 developer preview version, bundling was introduced with two transforms JsMinify and CssMinify which minify JavaScript & CSS respectively. Then after community came with few more transforms for Sass, LESS, CoffeeScript, (TypeScript might be in pipeline) etc. But if you have noticed, one thing is common in all these transforms is that all transforms are applying on files. We include some files in bundle with Bundle.Include and those files are transformed. In other word, we could pass only file (or VirtualFile) to bundle transform and bundle transform read those file content and process it. But what if I want to pass other information to bundle transform? Let say I want to pass Dictionary collection to bundle transform. At first glance, we don’t have any mechanism for the same! Is it really so? Nop we can pass such extra parameter to bundle transform and can utilize it while generating bundle response. Following is the steps for the same.

Create one class which is derived from Bundle class and add properties for the any extra parameter in it as displayed below.

public class CustomBundle : Bundle
{
    private Dictionary<string, string> _metaInfo = null;
 
    public CustomBundle(string virtualPath)
        : base(virtualPath, new CustomTransform())
    {
    }
 
    public Dictionary<string, string> MetaInfo
    {
        get { return _metaInfo; }
        set { _metaInfo = value; }
    }
}

For demonstration purpose, I have created one property of type dictionary but it could be of any type. With the help of this property, we will pass additional info to bundle transform.

With custom bundle class we have defined list of additional parameters. Now following is the code of custom transform type and how we can read those extra parameters with the help of bundle context while generating response. For more info on custom transform type read my previous post here.

public class CustomTransform : IBundleTransform
{
    public void Process(BundleContext context, BundleResponse response)
    {
        CustomBundle bundle = (context.BundleCollection
            .Where(b => b.Path == context.BundleVirtualPath)
            .FirstOrDefault()) as CustomBundle;
 
        response.Content = JsonConvert.SerializeObject(bundle.MetaInfo);
    }
}

As we know that we can have access of bundle collection while transforming bundle. So we are extracting current bundle from bundle collection and hence type of current bundle is CustomBundle we can access extra parameters through it. Here for simplicity we are serializing it into JSON but possibilities are lot more stay tuned for tomorrow’s post to know how I wanted to use it.

Now we are ready with custom bundle and transform type so following is the code which shows how to add this custom bundle to bundle collection.

CustomBundle bundle = new CustomBundle("~/bundles/metainfo");
 
bundle.MetaInfo = new Dictionary<string, string>();
bundle.MetaInfo.Add("Blog", "dotnetExpertGuide.com");
bundle.MetaInfo.Add("Author", "Nandip Makwana");
 
bundles.Add(bundle);

What leads me to find the way of passing extra parameters and how I wanted to use it?

For simplicity and clear separation of topic I have written another post which shows how I wanted to use this bundling approach with ASP.NET MVC application.

Check out this index post on ASP.NET Web Optimization Framework (a.k.a Bundling & Minification) to read other post on same.

You can follow me on twitter for latest link and update on ASP.NET & MVC.

comments powered by Disqus

Featured Content

Resources & Tools

About Nandip Makwana

Nandip Makwana is passionate about digital world and web. He completed his Masters in Computer Application in June 2011. Currently he is working as a Software Engineer. He has shown great promise and command over ASP.NET and technologies surrounding it during his academic years and professorial life...continue reading