Ejemplo n.º 1
0
        /// <summary>
        /// Asynchronously removes the script from the render pipeline and stores it into the HTML context to be rendered later.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="output">The output.</param>
        /// <returns></returns>
        public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
        {
            //Validate inputs
            var hasName = null != Name && _namePatern.IsMatch(Name);
            var hasSrc  = !string.IsNullOrWhiteSpace(Src);

            if (!hasName && !hasSrc)
            {
                throw new ArgumentException("Name is required.  It must be a single string without whitespace, commas, pipes or semi-colons.", nameof(Name));
            }
            var namedScript = new NamedScriptInfo {
                Name = Name ?? Src, Src = Src, Dependencies = _dependsOn, Aliases = _aliases
            };

            if (hasSrc)
            {
                if (!Src.EndsWith(".min.js"))
                {
                    //TODO:  Consider automatically looking at a minified source cache
                }
            }
            else
            {
                //Get the script contents
                var contents = await output.GetChildContentAsync(true);

                var scriptContent = contents.GetContent();
                namedScript.Script = scriptContent;
            }

            //Save them into the http Context
            if (_httpContextAccessor.HttpContext.Items.ContainsKey(ViewDataKey))
            {
                var scripts = (IDictionary <string, NamedScriptInfo>)_httpContextAccessor.HttpContext.Items[ViewDataKey];
                if (scripts.ContainsKey(namedScript.Name))
                {
                    Debug.WriteLine("Duplicate script ignored");
                }
                else
                {
                    scripts.Add(namedScript.Name, namedScript);
                }
            }
            else
            {
                _httpContextAccessor.HttpContext.Items[ViewDataKey] = new Dictionary <string, NamedScriptInfo> {
                    { namedScript.Name, namedScript }
                }
            };

            //suppress any output
            output.SuppressOutput();
        }
    }
        /// <summary>
        /// Asynchronously removes the script from the render pipeline and stores it into the HTML context to be rendered later.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="output">The output.</param>
        /// <returns></returns>
        public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
        {
            //Validate inputs
            var hasName = null != Name && _namePatern.IsMatch(Name);
            var hasSrc = !string.IsNullOrWhiteSpace(Src);
            if (!hasName && !hasSrc)
                throw new ArgumentException("Name is required.  It must be a single string without whitespace, commas, pipes or semi-colons.", nameof(Name));
            var namedScript = new NamedScriptInfo { Name = Name ?? Src, Src = Src, Dependencies = _dependsOn, Aliases = _aliases };
            if (hasSrc)
            {
                if (!Src.EndsWith(".min.js"))
                {
                    //TODO:  Consider automatically looking at a minified source cache
                }
            }
            else
            {
                //Get the script contents
                var contents = await output.GetChildContentAsync(true);
                var scriptContent = contents.GetContent();
                namedScript.Script = scriptContent;
            }

            //Save them into the http Context
            if (_httpContextAccessor.HttpContext.Items.ContainsKey(ViewDataKey))
            {
                var scripts = (IDictionary<string, NamedScriptInfo>)_httpContextAccessor.HttpContext.Items[ViewDataKey];
                if (scripts.ContainsKey(namedScript.Name))
                    Debug.WriteLine("Duplicate script ignored");
                else
                    scripts.Add(namedScript.Name, namedScript);
            }
            else
                _httpContextAccessor.HttpContext.Items[ViewDataKey] = new Dictionary<string, NamedScriptInfo> { { namedScript.Name, namedScript } };

            //suppress any output
            output.SuppressOutput();
        }