Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove querysting token of version generator using bundles in mvc 4

Hi i m using mvc4 application and using bundles to render scripts and css

below is how i created the bundle:

bundles.Add( new StyleBundle( "~/Content/css" ).Include(
                    "~/Content/bootstrap/css/bootstrap.css",
                    "~/Content/bootstrap/css/bootstrap-responsive.css",
                    "~/Content/site.css" ) );

Now it generates the bundle like this as i see in browser source:

<link href="/Content/css?v=8HZAB6C8ZnrIPYNFzMQKt0AR4AUsUYBjxPPkbGSRIZo1" rel="stylesheet">

What i want is to remove the "v" query string of version in the link.

Thanks

like image 798
Rusty Avatar asked Dec 08 '22 18:12

Rusty


2 Answers

I have created an extension for the mvc-bundling, that enables you to move the querystring part inside the url instead. It can generate urls like this

/bundles/css/20130315191550.css

/bundles/css/_NNIf4XxdPCITzjlKPMgZwHMSUsPyxxGaNCIe6mgAkg1

Code available at http://github.com/unger/Bundling.Extensions

Maybe this can give you some idea on how to solve it?

like image 194
unger Avatar answered Dec 11 '22 07:12

unger


I ended up doing the followings:

First I changed the default BundleResolver with this implementation

public class ReplaceQueryStringBundlerResolver : IBundleResolver
        {
            private readonly IBundleResolver _resolver;

            public ReplaceQueryStringBundlerResolver(IBundleResolver resolver)
            {
                _resolver = resolver;
            }

            public IEnumerable<string> GetBundleContents(string virtualPath)
            {
                return _resolver.GetBundleContents(virtualPath);
            }

            //The important part, modifies the generated Url
            public string GetBundleUrl(string virtualPath)
            {
                var bundleUrl = _resolver.GetBundleUrl(virtualPath);
                bundleUrl = bundleUrl.Replace("?v=", "/v/");
                return bundleUrl;
            }

            public bool IsBundleVirtualPath(string virtualPath)
            {
                return _resolver.IsBundleVirtualPath(virtualPath);
            }
        }

And this can be registered in RegisterBundles method like this.

 BundleResolver.Current = new ReplaceQueryStringBundlerResolver(BundleResolver.Current);

Which basically instructs the current resolver to use our needed Url format.

This solution is better because we don't have to change Styles.Render or Scripts.Render calls anywhere in the calling code.

Since we need to map these new bundling Urls to the bundles tables, I tried to do something similar with the current Api, but I couldn't get it working so as a temporary solution I added the rewrite rules in the web.config so we can rewrite /v/ to ?v=. Ideally the optimization module should do this, since the rewrite rules restrict us to follow some naming conventions and mine is that all bundles virtual paths should start with "~/bundles/"

<rule name="RewriteBundlesWithNoQueryStrings" stopProcessing="true">
  <match url="^bundles/(.*)/v/(.*)" />
  <action type="Rewrite" url="/bundles/{R:1}?v={R:2}" />
</rule>
<rule name="RewriteBundlesWithNoQueryStringsToDefault" stopProcessing="true">
  <match url="^bundles/(.*)/(.*)" />
  <action type="Rewrite" url="/bundles/{R:1}" />
</rule>

If I can hook something in the BundleHandler class which handles the bundlings requests, I'll come up with an update.

like image 37
Adrian Iftode Avatar answered Dec 11 '22 06:12

Adrian Iftode