How to get the software token of the package version?

We are developing Main WebApp with angularJS as a single page application for the Cordova mobile application.

We moved part of the static resources to a couple of packages that will be served from another WebApp CDN from another domain.

We do not use the helper helper @Scripts.Render @Styles.Render , because the bundles directly reference the built-in static index.html inside a mobile application like this (added via AngularJS):

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

Since we do not use a razor, we do not add a cache token to src, and this is not what we want; we need a version token to force the client to download an updated version of the package.
I read in a previous post that the v token is computed every time using Scripts.Render.

Now the question is:
Is it possible for programmatic access to the value of this token?

We would like to create a service controller, which, given the route of the bundles, returns the packet SHA256 token.
Once received, it will be used to create script tags that will be added dynamically 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 own token ourselves (for example, using the build number), but it would be nice to have something with less effort and become more attached to the connection mechanism.

+6
source share
1 answer

Here is a brief example of getting a v token from a virtual bundle.

 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 not a script bundle, check if it 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"]; } } 
+5
source

Source: https://habr.com/ru/post/975289/


All Articles