Exemple #1
0
        internal BundlingOptions(BundlingOptions options, BundlingConfiguration configuration) : base(new SharedOptions())
        {
            BundleManager                = options.BundleManager;
            SourceFileProvider           = configuration.SourceFileProvider ?? options.SourceFileProvider;
            CaseSensitiveSourceFilePaths = configuration.CaseSensitiveSourceFilePaths ?? options.CaseSensitiveSourceFilePaths;
            StaticFilesRequestPath       = configuration.StaticFilesPathPrefix ?? options.StaticFilesRequestPath;

            ContentTypeProvider   = options.ContentTypeProvider;
            DefaultContentType    = options.DefaultContentType;
            ServeUnknownFileTypes = options.ServeUnknownFileTypes;
            OnPrepareResponse     = options.OnPrepareResponse;

            RequestPath  = configuration.BundlesPathPrefix ?? options.RequestPath;
            FileProvider = options.FileProvider;
        }
        public BundlingMiddleware(RequestDelegate next, IWebHostEnvironment 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;

            BundlingOptions 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);

            BundleGlobalOptions globalOptionsUnwrapped = globalOptions.Value;

            if (globalOptionsUnwrapped.EnableCacheHeader)
            {
                Action <StaticFileResponseContext> originalPrepareResponse = optionsUnwrapped.OnPrepareResponse;
                optionsUnwrapped.OnPrepareResponse = ctx =>
                {
                    Microsoft.AspNetCore.Http.Headers.ResponseHeaders headers = ctx.Context.Response.GetTypedHeaders();
                    headers.CacheControl = new CacheControlHeaderValue {
                        MaxAge = globalOptionsUnwrapped.CacheHeaderMaxAge
                    };
                    originalPrepareResponse?.Invoke(ctx);
                };
            }

            _staticFileMiddleware = new StaticFileMiddleware(next, env, options, loggerFactory);
        }