示例#1
0
        private void UpdateHash(FileVersionHash withHash, IEnumerable <IFileProvider> fileProviders)
        {
            if (this.HashType != withHash)
            {
                this.HashType = withHash;
                switch (this.HashType)
                {
                case FileVersionHash.FileHash:
                    var file = fileProviders.Select(x => x.GetFileInfo(this.webRootRelativePath)).FirstOrDefault(x => x.Exists);
                    if (file == null)
                    {
                        throw new Exception($"Script file {this.webRootRelativePath} could not be found!");
                    }

                    using (var md5 = System.Security.Cryptography.MD5.Create())
                        using (var fileStream = file.CreateReadStream())
                        {
                            var hash = md5.ComputeHash(fileStream);
                            this.FileHash = BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant();
                        }
                    break;

                case FileVersionHash.Random:
                    this.FileHash = new Random().Next().ToString();
                    break;

                case FileVersionHash.None:
                    this.FileHash = "";
                    break;
                }
            }
        }
示例#2
0
        public HtmlString Render(string staticRoot, IEnumerable <IFileProvider> fileProviders, FileVersionHash withHash = FileVersionHash.None)
        {
            string hash = "";

            this.UpdateHash(withHash, fileProviders);

            if (withHash != FileVersionHash.None)
            {
                hash = $"?hash={this.FileHash}";
            }

            return(new HtmlString($"<link href=\"{staticRoot.TrimEnd('/')}/{this.webRootRelativePath.TrimStart('~', '/')}{hash}\" rel=\"stylesheet\" media=\"{this.MediaQuery}\" />"));
        }