public BundlingMiddleware(RequestDelegate next, IHostingEnvironment env, ILoggerFactory loggerFactory, IHttpContextAccessor httpContextAccessor,
            IOptions<BundleGlobalOptions> globalOptions, IBundleManagerFactory bundleManagerFactory, BundleCollection bundles, IOptions<BundlingOptions> options)
        {
            if (next == null)
                throw new ArgumentNullException(nameof(next));

            if (env == null)
                throw new ArgumentNullException(nameof(env));

            if (loggerFactory == null)
                throw new ArgumentNullException(nameof(loggerFactory));

            if (httpContextAccessor == null)
                throw new ArgumentNullException(nameof(httpContextAccessor));

            if (globalOptions == null)
                throw new ArgumentNullException(nameof(globalOptions));

            if (bundleManagerFactory == null)
                throw new ArgumentNullException(nameof(bundleManagerFactory));

            if (bundles == null)
                throw new ArgumentNullException(nameof(bundles));

            if (options == null)
                throw new ArgumentNullException(nameof(options));

            _next = next;

            var optionsUnwrapped = options.Value;

            _bundleManager = optionsUnwrapped.BundleManager ?? bundleManagerFactory.Create(bundles, new BundlingContext
            {
                BundlesPathPrefix = optionsUnwrapped.RequestPath,
                StaticFilesPathPrefix = optionsUnwrapped.StaticFilesRequestPath
            });

            optionsUnwrapped.FileProvider = optionsUnwrapped.FileProvider ?? new BundleFileProvider(_bundleManager, httpContextAccessor);

            var globalOptionsUnwrapped = globalOptions.Value;
            if (globalOptionsUnwrapped.EnableCacheHeader)
            {
                var originalPrepareResponse = optionsUnwrapped.OnPrepareResponse;
                optionsUnwrapped.OnPrepareResponse = ctx =>
                {
                    var headers = ctx.Context.Response.GetTypedHeaders();
                    headers.CacheControl = new CacheControlHeaderValue { MaxAge = globalOptionsUnwrapped.CacheHeaderMaxAge };
                    originalPrepareResponse?.Invoke(ctx);
                };
            }

            _staticFileMiddleware = new StaticFileMiddleware(next, env, options, loggerFactory);
        }
Example #2
0
        public BundlingTagHelperBase(IBundleManagerFactory bundleManagerFactory, IUrlHelperFactory urlHelperFactory)
        {
            if (bundleManagerFactory == null)
            {
                throw new ArgumentNullException(nameof(bundleManagerFactory));
            }

            if (urlHelperFactory == null)
            {
                throw new ArgumentNullException(nameof(urlHelperFactory));
            }

            _bundleManagerFactory = bundleManagerFactory;

            _urlHelperFactory = urlHelperFactory;
        }
Example #3
0
        static async Task <string> GenerateUrlCoreAsync(string pathString, IBundleManagerFactory bundleManagerFactory, HttpContext httpContext)
        {
            var actionContext = new ActionContext(httpContext, dummyRouteData, dummyActionDescriptor);
            var urlHelper     = new UrlHelper(actionContext);

            UrlUtils.FromRelative(urlHelper.Content(pathString), out PathString path, out QueryString query, out FragmentString fragment);

            string url = null;
            var    n   = bundleManagerFactory.Instances.Count;

            for (var i = 0; i < n; i++)
            {
                if ((url = await bundleManagerFactory.Instances[i].TryGenerateUrlAsync(path, query, httpContext)) != null)
                {
                    break;
                }
            }

            return(url ?? pathString);
        }
Example #4
0
        public static async Task <IHtmlContent> UrlAsync(string path)
        {
            if (path == null)
            {
                throw new ArgumentNullException(nameof(path));
            }

            HttpContext httpContext = HttpContextStatic.Current;

            if (httpContext == null)
            {
                throw ErrorHelper.HttpContextNotAvailable();
            }

            IBundleManagerFactory bundleManagerFactory = httpContext.RequestServices.GetRequiredService <IBundleManagerFactory>();

            var result = await GenerateUrlCoreAsync(path, bundleManagerFactory, httpContext);

            return(new HtmlString(result));
        }
Example #5
0
        public static async Task <IHtmlContent> RenderFormatAsync(string tagFormat, params string[] paths)
        {
            if (paths == null)
            {
                throw new ArgumentNullException(nameof(paths));
            }

            if (Array.FindIndex(paths, p => p == null) >= 0)
            {
                throw ErrorHelper.ArrayCannotContainNull(nameof(paths));
            }

            HttpContext httpContext = HttpContextStatic.Current;

            if (httpContext == null)
            {
                throw ErrorHelper.HttpContextNotAvailable();
            }

            IBundleManagerFactory bundleManagerFactory = httpContext.RequestServices.GetRequiredService <IBundleManagerFactory>();

            var generateTasks = new List <Task <string> >();

            foreach (var path in paths)
            {
                generateTasks.Add(GenerateUrlCoreAsync(path, bundleManagerFactory, httpContext));
            }

            await Task.WhenAll(generateTasks);

            var sb = new StringBuilder();
            var n  = generateTasks.Count;

            for (var i = 0; i < n; i++)
            {
                sb.AppendFormat(tagFormat, generateTasks[i].Result);
                sb.AppendLine();
            }
            return(new HtmlString(sb.ToString()));
        }
Example #6
0
 public BundlingLinkTagHelper(IBundleManagerFactory bundleManagerFactory, IUrlHelperFactory urlHelperFactory)
     : base(bundleManagerFactory, urlHelperFactory)
 {
 }