6

We are developing a Main WebApp with angularJS as a Single Page application for a Cordova mobile App.

We have moved part of the static resources into a couple of bundles that will be served from a different CDN WebApp from another domain.

We are not using the @Scripts.Render @Styles.Render razor helper because bundles are directly referenced from the embedded static index.html inside the mobile app like this (Appended via AngularJS):

<script src="https://service.foo.it/CDN/cdnFooJs"></script>
<script src="https://service.foo.it/CDN/cdnFooCss"></script>

As we are not using razor, we are not appending any cache token to the src and that's not what we want; we need a version token to force the client to download the updated version of the bundle.
I've read in some previous post that the v token is calculated every time Scripts.Render is used.

Now, the question is:
is it possible, to access the value of this token programmatically ?

We would like to create a service controller that, given a bundles route, returns the SHA256 token of the bundle.
Once received,it will be used to build the script tags that will be appended dinamically to the DOM.

<script src="https://service.foo.it/CDN/cdnFooJs?vtoken=asd3...."></script>
<script src="https://service.foo.it/CDN/cdnFooCss?vtoken=dasdasrq..."></script>

Note:
We already know that we can create our token by ourselves (for example using the build number), but it would nice to have something with less effort and more tied to the bundle mechanism.

Community
  • 1
  • 1
systempuntoout
  • 71,966
  • 47
  • 171
  • 241
  • You want to _create a service controller_, is that means you are going to make a http request just for getting the version token? Or you prefer a htmlhelper method that you can directly use in view? Getting the token itself can be really simple as long as you have the virtual path. – tweray Sep 15 '14 at 13:17
  • I'm going to make an http request just for getting the version token. – systempuntoout Sep 15 '14 at 13:23

1 Answers1

5

Here's a brief example of getting the v token from virtual bundle path.

public class BundleTokenController : ApiController
{
    public string Get(string path)
    {
        var url = System.Web.Optimization.Scripts.Url(path).ToString(); 
        //This will return relative url of the script bundle with querystring

        if (!url.Contains("?"))
        {
            url = System.Web.Optimization.Styles.Url(path).ToString(); 
            //If it's not a script bundle, check if it's a css bundle
        }

        if (!url.Contains("?"))
        {
            throw new Exception("Invalid path"); 
            //If neither, the path is invalid, 
            //or something going wrong with your bundle config, 
            //do error handling correspondingly
        }

        return GetTokenFromUrl(url);
    }

    private static string GetTokenFromUrl(string url)
    {
        //Just a raw way to extract the 'v' token from the relative url, 
        //there can be other ways

        var querystring = url.Split('?')[1];

        return HttpUtility.ParseQueryString(querystring)["v"];
    }
}
tweray
  • 1,002
  • 11
  • 18
  • I think you misunderstood the question; as we are not using razor, our src scripts do not have the cache token. – systempuntoout Sep 15 '14 at 14:26
  • @systempuntoout the bundling has no business with razor view engine. As long as your backend service is on asp.net, you can config a bundle and use it with or without the querystring, and you can get its url as long as you know the configured virtual path. The question is where exactly do you need the token, from my understanding you need the token fetched by a http call, and that's why I wrote a Apicontrolelr for you. If this is not what you want, please clarify what exactly you like it to work. – tweray Sep 15 '14 at 14:40
  • @systempuntoout which part of my code is razor? It is pure C# code and System.Web.Optimization is a namespace under System.Web.Optimization assembly in .net 4.0/4.5. If your backend is not in .net framework, then story will change. Otherwise as I said, the bundling has nothing to do with razor view engine. The only 2 things you need for accessing the bundle are only: 1. You referenced the necessary assembly on server end; 2. You configured the bundle – tweray Sep 15 '14 at 17:02
  • 1
    please, read the question again. Since we are not using `@Scripts.Render` or `@Style.Render` as we are directly accessing the bundle from a static html page, we are not getting the script tag rendered with the v token. What we need is a way to know the v token given a bundle route. – systempuntoout Sep 15 '14 at 19:00
  • @systempuntoout, forgive me if I am slow on understanding. Let's confirm things one by one. First, when you mention `given a bundle route`. Is that `bundle route` something like `https://service.foo.it/CDN/cdnFooJs`? Or something like `~/CDN/cndFooJs`, or something else? – tweray Sep 15 '14 at 19:43
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/61300/discussion-between-tweray-and-systempuntoout). – tweray Sep 15 '14 at 19:49
  • you were right. I totally misunderstood your answer!Enjoy the Rep! – systempuntoout Sep 16 '14 at 07:33