Пример #1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllersWithViews();


            var cssSettings  = new CssBundlingSettings();
            var codeSettings = new CodeBundlingSettings
            {
                Minify = true,
            };

            services.AddWebOptimizer(HostingEnvironment, cssSettings, codeSettings);
        }
Пример #2
0
        /// <summary>
        /// Adds WebOptimizer to the specified <see cref="IServiceCollection"/> and enables CSS and JavaScript minification.
        /// </summary>
        /// <param name="services">Collection of service descriptors</param>
        public static void AddNopWebOptimizer(this IServiceCollection services)
        {
            var appSettings = Singleton <AppSettings> .Instance;
            var cssBundling = appSettings.Get <WebOptimizerConfig>().EnableCssBundling;
            var jsBundling  = appSettings.Get <WebOptimizerConfig>().EnableJavaScriptBundling;

            //add minification & bundling
            var cssSettings = new CssBundlingSettings
            {
                FingerprintUrls = false,
                Minify          = cssBundling
            };

            var codeSettings = new CodeBundlingSettings
            {
                Minify = jsBundling,
                AdjustRelativePaths = false //disable this feature because it breaks function names that have "Url(" at the end
            };

            services.AddWebOptimizer(null, cssSettings, codeSettings);
        }
Пример #3
0
        public override void ConfigureServices(IServiceCollection services, IApplicationContext appContext, bool isActiveModule)
        {
            // Add action context accessor
            services.AddTransient <IActionContextAccessor, ActionContextAccessor>();

            if (appContext.IsInstalled)
            {
                // Configure Cookie Policy Options
                services.AddSingleton <IConfigureOptions <CookiePolicyOptions>, CookiePolicyOptionsConfigurer>();

                services.Configure <RazorViewEngineOptions>(o =>
                {
                    o.ViewLocationExpanders.Add(new ThemeViewLocationExpander());
                    o.ViewLocationExpanders.Add(new AdminViewLocationExpander());
                    o.ViewLocationExpanders.Add(new PartialViewLocationExpander());

                    if (appContext.AppConfiguration.EnableLocalizedViews)
                    {
                        o.ViewLocationExpanders.Add(new LanguageViewLocationExpander(LanguageViewLocationExpanderFormat.Suffix));
                    }

                    // TODO: (core) Implement ModuleViewLocationExpander
                });
            }

            // Add AntiForgery
            services.AddAntiforgery(o =>
            {
                o.Cookie.Name = CookieNames.Antiforgery;
                o.HeaderName  = "X-XSRF-Token";
            });

            // Add HTTP client feature
            services.AddHttpClient();

            // Add session feature
            services.AddSession(o =>
            {
                o.Cookie.Name        = CookieNames.Session;
                o.Cookie.IsEssential = true;
            });

            // Detailed database related error notifications
            services.AddDatabaseDeveloperPageExceptionFilter();

            services.Configure <WebEncoderOptions>(o =>
            {
                o.TextEncoderSettings = new TextEncoderSettings(UnicodeRanges.All);
            });

            // TODO: (core) Implement localization stuff
            //services.TryAddSingleton<IStringLocalizerFactory, SmartStringLocalizerFactory>();
            //services.TryAddScoped(typeof(IStringLocalizer<>), typeof(SmartStringLocalizer<>));

            services.AddRouting(o =>
            {
                // TODO: (core) Make this behave like in SMNET
                o.AppendTrailingSlash = true;
                o.LowercaseUrls       = true;
            });

            var mvcBuilder = services
                             .AddControllersWithViews(o =>
            {
                //o.EnableEndpointRouting = false;
                // TODO: (core) AddModelBindingMessagesLocalizer
                o.Filters.AddService <IViewDataAccessor>(int.MinValue);

                // TODO: (core) Add more model binders
                var complexBinderProvider = o.ModelBinderProviders.OfType <ComplexObjectModelBinderProvider>().First();
                o.ModelBinderProviders.Insert(0, new GridCommandModelBinderProvider(complexBinderProvider));
            })
                             .AddRazorRuntimeCompilation(o =>
            {
                // TODO: (core) FileProvider
            })
                             .AddFluentValidation(c =>
            {
                c.LocalizationEnabled = true;
                c.ImplicitlyValidateChildProperties = true;

                // Scan active assemblies for validators
                c.RegisterValidatorsFromAssemblies(appContext.TypeScanner.Assemblies, lifetime: ServiceLifetime.Scoped);

                var opts = c.ValidatorOptions;

                // It sais 'not recommended', but who cares: SAVE RAM!
                opts.DisableAccessorCache = true;

                // Language Manager
                opts.LanguageManager = new ValidatorLanguageManager(appContext);

                // Display name resolver
                var originalDisplayNameResolver = opts.DisplayNameResolver;
                opts.DisplayNameResolver        = (type, member, expression) =>
                {
                    string name = null;

                    if (expression != null && member != null)
                    {
                        var metadataProvider = EngineContext.Current.Application.Services.Resolve <IModelMetadataProvider>();
                        var metadata         = metadataProvider.GetMetadataForProperty(member.DeclaringType, member.Name);
                        name = metadata.DisplayName;
                    }

                    return(name ?? originalDisplayNameResolver.Invoke(type, member, expression));
                };
            })
                             .AddNewtonsoftJson(o =>
            {
                var settings = o.SerializerSettings;
                settings.ContractResolver       = SmartContractResolver.Instance;
                settings.TypeNameHandling       = TypeNameHandling.Objects;
                settings.ReferenceLoopHandling  = ReferenceLoopHandling.Ignore;
                settings.ObjectCreationHandling = ObjectCreationHandling.Replace;
                settings.NullValueHandling      = NullValueHandling.Ignore;
                settings.MaxDepth = 32;
                //settings.DateFormatHandling = DateFormatHandling.MicrosoftDateFormat;
            })
                             .AddControllersAsServices()
                             .AddAppLocalization()
                             .AddMvcOptions(o =>
            {
                // TODO: (core) More MVC config?
                if (DataSettings.DatabaseIsInstalled())
                {
                    // Register custom metadata provider
                    o.ModelMetadataDetailsProviders.Add(new SmartDisplayMetadataProvider());
                    o.ModelMetadataDetailsProviders.Add(new AdditionalMetadataProvider());
                }
            });

            // Add TempData feature
            if (appContext.AppConfiguration.UseCookieTempDataProvider)
            {
                mvcBuilder.AddCookieTempDataProvider(o =>
                {
                    o.Cookie.Name        = CookieNames.TempData;
                    o.Cookie.IsEssential = true;
                });
            }
            else
            {
                mvcBuilder.AddSessionStateTempDataProvider();
            }

            // Replace BsonTempDataSerializer that was registered by AddNewtonsoftJson()
            // with our own serializer which is capable of serializing more stuff.
            services.AddSingleton <TempDataSerializer, SmartTempDataSerializer>();

            var cssBundlingSettings = new CssBundlingSettings {
                Minify = false
            };
            var codeBundlingSettings = new CodeBundlingSettings {
                Minify = false
            };
            var codeSettings = new CodeSettings {
                IgnoreAllErrors = false, MinifyCode = false, ScriptVersion = ScriptVersion.EcmaScript6, EvalLiteralExpressions = false, AmdSupport = true
            };

            codeSettings.IgnoreErrorCollection.Add("JS1010");

            services.AddWebOptimizer((IWebHostEnvironment)appContext.HostEnvironment, cssBundlingSettings, codeBundlingSettings, p => {
                var asset = p.AddJavaScriptBundle("/bundle/js/datagrid.js",
                                                  "components/datagrid/datagrid.js",
                                                  "components/datagrid/datagrid-pager.js",
                                                  "components/datagrid/datagrid-tools.js",
                                                  "js/smartstore.editortemplates.js")
                            .Concatenate()
                            //.MinifyJavaScript(codeSettings)
                            .FingerprintUrls();
            });
        }