Exemplo n.º 1
0
        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            output.TagName = null;

            if (string.IsNullOrEmpty(Path))
            {
                throw new ArgumentNullException("Path", "Path must be provided");
            }

            try
            {
                var dependencyLoader = DependencyManager.GetLoader(_httpContextAccessor.HttpContext);
                
                if (!dependencyLoader.DependencyFiles.Any(d => d.FilePath.ToLower() == Path.ToLower()))
                {
                    if (HasEnvironment())
                    {
                        dependencyLoader.DependencyFiles.Add(new DependencyFile
                        {
                            DependencyType = Type,
                            ScriptLocation = Location != null ? Location : ScriptLocation.BodyEnd,
                            FilePath = Path,
                            Priority = Priority > 0 ? Priority : Priority + 100,
                            Attributes = output.Attributes.ToDictionary(k => k.Name, v => v.Value)
                        });
                    }
                }
                //output.Content.SetContent(null);
                output.SuppressOutput();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Exemplo n.º 2
0
        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            try
            {
                output.TagName = null;
                var dependencyLoader = DependencyManager.GetLoader(_httpContextAccessor.HttpContext);
                //var fileVersionProvider = new FileVersionProvider(
                //    _hostingEnvironment.WebRootFileProvider,
                //    _cache,
                //    ViewContext.HttpContext.Request.PathBase);
                //var sb = new StringBuilder();
                var dependencyFiles = dependencyLoader.DependencyFiles
                                      .Where(df => df.DependencyType == DependencyType)
                                      .OrderBy(df => df.Priority)
                                      .ToList();
                if (DependencyType == DependencyType.Css)
                {
                    foreach (var file in dependencyFiles)
                    {
                        var    itemBuilder = new TagBuilder("link");
                        string resolvedPath;

                        if (file.FilePath.StartsWith("~"))
                        {
                            TryResolveUrl(file.FilePath, out resolvedPath);
                            //resolvedPath = fileVersionProvider.AddFileVersionToPath(resolvedPath);
                        }
                        else
                        {
                            resolvedPath = file.FilePath;
                        }


                        itemBuilder.MergeAttributes(file.Attributes);
                        itemBuilder.Attributes.Add("href", resolvedPath);
                        itemBuilder.Attributes.Add("rel", "stylesheet");
                        output.Content.AppendHtml(itemBuilder);
                        output.Content.AppendHtml(Environment.NewLine);
                    }
                }
                else if (DependencyType == DependencyType.Script)
                {
                    var filteredFiles = dependencyFiles.Where(df => df.ScriptLocation == ScriptLocation).ToList();

                    foreach (var file in filteredFiles)
                    {
                        var    itemBuilder = new TagBuilder("script");
                        string resolvedPath;

                        if (file.FilePath.StartsWith("~"))
                        {
                            TryResolveUrl(file.FilePath, out resolvedPath);
                            //resolvedPath = fileVersionProvider.AddFileVersionToPath(resolvedPath);
                        }
                        else
                        {
                            resolvedPath = file.FilePath;
                        }

                        itemBuilder.MergeAttributes(file.Attributes);
                        itemBuilder.Attributes.Add("src", resolvedPath);
                        output.Content.AppendHtml(itemBuilder);
                        output.Content.AppendHtml(Environment.NewLine);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }