示例#1
0
        public static void SetupHttpsRedirection(this IApplicationBuilder app, IConfiguration config)
        {
            if (bool.Parse(config["Https:Enabled"]))
            {
                app.UseHttpsRedirection();
            }

            var options = new RewriteOptions().Add(c =>
            {
                if (c.HttpContext.Request.Path.Equals("/tag") && c.HttpContext.Request.Query.ContainsKey("tag"))
                {
                    c.Result = RuleResult.EndResponse;
                    c.HttpContext.Response.Redirect("/tag/" + HttpUtility.UrlEncode(c.HttpContext.Request.Query["tag"]), true);
                }

                if ((c.HttpContext.Request.Path.Equals("/search") || c.HttpContext.Request.Path.Equals("/s")) && c.HttpContext.Request.Query.ContainsKey("wd"))
                {
                    c.Result = RuleResult.EndResponse;
                    c.HttpContext.Response.Redirect("/search/" + HttpUtility.UrlEncode(c.HttpContext.Request.Query["wd"]).Replace("+", "%20"), true);
                }
            }).AddRewrite(@"\w+/_blazor(.*)", "_blazor$1", false);

            switch (config["UseRewriter"])
            {
            case "NonWww":
                options.AddRedirectToNonWww(301);     // URL重写
                break;

            case "WWW":
                options.AddRedirectToWww(301);     // URL重写
                break;
            }

            app.UseRewriter(options);
        }
        /// <summary>
        /// Adds middleware for redirecting with out WWW Requests to WWW.
        /// </summary>
        /// <param name="app">The <see cref="IApplicationBuilder"/> instance this method extends.</param>
        /// <returns>The <see cref="IApplicationBuilder"/> for WWWRedirection.</returns>
        public static IApplicationBuilder UseWwwRedirection(this IApplicationBuilder app)
        {
            if (app == null)
            {
                throw new ArgumentNullException(nameof(app));
            }

            var options = new RewriteOptions();

            options.AddRedirectToWww();
            app.UseRewriter(options);
            return(app);
        }
示例#3
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseCookiePolicy();
            app.UseSession();
            app.UseResponseCaching();
            app.UseAuthentication();

            //
            var options = new RewriteOptions();

            options.AddRedirectToWww();

            options.AddRedirectToHttps();
            app.UseRewriter(options);



            //


            app.UseSignalR(routes =>
            {
                routes.MapHub <ChatHub>("/chatHub");
            });
            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
        }
示例#4
0
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseHsts();
            }
            if (isProd)
            {
                var options = new RewriteOptions();
                options.AddRedirectToWww();
                options.AddRedirectToHttps();
                app.UseRewriter(options);
            }

            app.UseStaticFiles();
            app.UseSpaStaticFiles();

            app.UseMvc(routes => {
                routes.MapRoute(
                    name: "default",
                    template: "{controller}/{action=Index}/{id?}");
            });

            app.UseSpa(spa => {
                spa.Options.SourcePath = "dist";
                if (env.IsDevelopment())
                {
                    spa.Options.SourcePath = "../CodeStack.UI/dist";
                    spa.UseAngularCliServer(npmScript: "start");
                }
            });
        }
示例#5
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            if (!Directory.Exists(Path.Combine(env.ContentRootPath, "Logs")))
            {
                try
                {
                    Directory.CreateDirectory(Path.Combine(env.ContentRootPath, "Logs"));
                }
                catch (Exception)
                {
                    Console.WriteLine("Startup: Error creating log file directory.");
                }
            }

            loggerFactory.AddFile(Path.Combine("Logs", "_log-{Date}.txt"));
            ILogger logger = loggerFactory.CreateLogger("Startup");

            app.UseResponseCompression();

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseWebpackDevMiddleware(new WebpackDevMiddlewareOptions
                {
                    HotModuleReplacement = true,
                });
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                //app.UseHsts();

                // Redirect non-www to www
                var rewriteOptions = new RewriteOptions();
                rewriteOptions.AddRedirectToWww();
                app.UseRewriter(rewriteOptions);
            }

            if (Configuration.GetSection("Options").GetValue <bool>("Https"))
            {
                app.UseHttpsRedirection();
            }
            else
            {
                Console.WriteLine("Https is disabled. Consider enabling it in the configuration file and providing a certificate.");
            }

            app.UseAuthentication();
            app.UseSession();

            //app.UseDefaultFiles();

            // Exposes everything in the /dist folder where all our front-end files have been placed through webpack
            app.UseWebpackFileServer(env, logger);

            // Exposes everything in /videos folder for serving video files
            // TODO: Some callback to the front-end to know to not expose the path for /videos would be useful if disabled.
            if (Configuration.GetValue <bool>("Options:VideoFiles"))
            {
                app.UseVideoFileServer(env, logger);
            }

            // Necessary for CertifyTheWeb to automatically re-authorize the webserver's TLS cert
            if (Configuration.GetValue <bool>("Options:Well-Known"))
            {
                app.UseAutoAuthorizerStaticFiles(env, logger);
            }

            app.Use(async(context, next) =>
            {
                if (!context.Response.Headers.ContainsKey("Cache-Control"))
                {
                    context.Response.Headers.Add("Cache-Control", "no-cache, no-store, must-revalidate;");
                    context.Response.Headers.Add("Pragma", "no-cache");
                }

                await next();
            });

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");

                routes.MapSpaFallbackRoute(
                    name: "spa-fallback",
                    defaults: new { controller = "Home", action = "Index" });
            });
        }