/// <summary> /// Configure the using of added middleware /// </summary> /// <param name="application">Builder for configuring an application's request pipeline</param> public void Configure(IApplicationBuilder application) { var serviceProvider = application.ApplicationServices; var grandConfig = serviceProvider.GetRequiredService <GrandConfig>(); var urlRewriteOptions = new RewriteOptions(); var rewriteOptions = false; if (grandConfig.UseUrlRewrite) { if (File.Exists("App_Data/UrlRewrite.xml")) { using (var streamReader = File.OpenText("App_Data/UrlRewrite.xml")) { rewriteOptions = true; urlRewriteOptions.AddIISUrlRewrite(streamReader); } } } if (grandConfig.UrlRewriteHttpsOptions) { rewriteOptions = true; urlRewriteOptions.AddRedirectToHttps(grandConfig.UrlRewriteHttpsOptionsStatusCode, grandConfig.UrlRewriteHttpsOptionsPort); } if (grandConfig.UrlRedirectToHttpsPermanent) { rewriteOptions = true; urlRewriteOptions.AddRedirectToHttpsPermanent(); } if (rewriteOptions) { application.UseRewriter(urlRewriteOptions); } }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostEnvironment env, ConfigurationsService configurations) { // Use static files app.UseStaticFiles(new StaticFileOptions { OnPrepareResponse = ctx => { ctx.Context.Response.Headers.Append("Cache-Control", "public, max-age=604800"); } }); // Uses routes app.UseRouting(); // Cors app.UseCors("Travian"); // Uses authentication app.UseAuthentication(); // Use authorization app.UseAuthorization(); // Uses session app.UseSession(); // Session handler app.UseMiddleware <SessionHandler>(); if (_isProduction) { // Show errors in page app.UseHsts(); // Error Handling app.UseStatusCodePages(); // Redirect options (www and https) var rewriteOptions = new RewriteOptions(); if (configurations.RedirectWww) { rewriteOptions.AddRedirectToWwwPermanent(); } if (configurations.RedirectHttps) { app.UseHttpsRedirection(); rewriteOptions.AddRedirectToHttpsPermanent(); } app.UseRewriter(rewriteOptions); } else { app.UseDeveloperExceptionPage(); } app.UseMiddleware <RedirectHandler>(); // Map routes app.UseEndpoints(endpoints => endpoints.MapControllers()); }
public static IApplicationBuilder UseConfiguredRewriter(this IApplicationBuilder app) { var options = new RewriteOptions(); options.AddRedirectToHttpsPermanent(); options.AddRedirectToWwwPermanent(); return(app.UseRewriter(options)); }
public void Configure(IApplicationBuilder app) { if (Env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Blog/Error"); app.UseHsts(); } var rewriteOptions = new RewriteOptions(); if (Configuration.GetValue <bool>("forceSsl")) { rewriteOptions.AddRedirectToHttpsPermanent(); } if (Configuration.GetValue <bool>("forceWwwPrefix")) { rewriteOptions.AddRedirectToWwwPermanent(); } app.UseRewriter(rewriteOptions); app.UseStaticFiles(); app.UseCookiePolicy(new CookiePolicyOptions()); app.UseAuthentication(); app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( name: "default", pattern: "{controller=Blog}/{action=Index}"); endpoints.MapControllerRoute( name: "post", pattern: "blog/post/{id}", defaults: new { controller = "Blog", action = "Post" }); endpoints.MapControllerRoute( name: "postEdit", pattern: "blog/editpost/{id?}", defaults: new { controller = "Blog", action = "EditPost" }); endpoints.MapControllerRoute( name: "postEdit", pattern: "blog/category/{name}", defaults: new { controller = "Blog", action = "Category" }); }); }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { AutofacContainer = app.ApplicationServices.GetAutofacRoot(); app.UseHeaderRemover("X-HTML-Minification-Powered-By"); app.UseForwardedHeaders(); app.UseClickJacking(); app.UseResponseCompression(); if (env.IsDevelopment()) { app.UseWebpackDevMiddleware(new WebpackDevMiddlewareOptions { HotModuleReplacement = true }); app.UseDeveloperExceptionPage(); } else { app.UseHsts(); } app.UseStatusCodePagesWithReExecute("/Error/{0}"); var reWriterOptions = new RewriteOptions() .Add(new RemoveTrailingSlash()); if (!env.IsDevelopment() && !env.IsEnvironment(IntegrationTestEnvironmentName)) { reWriterOptions.AddRedirectToHttpsPermanent(); } app.UseRewriter(reWriterOptions); app.UseResponseCaching(); app.UseStaticFiles(new StaticFileOptions { OnPrepareResponse = ctx => { if (!env.IsDevelopment()) { ctx.Context.Response.Headers.Add("Cache-Control", $"public,max-age={TimeConst.Year}"); ctx.Context.Response.Headers.Add("Access-Control-Allow-Origin", "*"); } } }); app.UseWebMarkupMin(); if (env.IsDevelopment() || env.IsStaging()) { app.UseSwagger(); // Enable middleWare to serve swagger-ui (HTML, JS, CSS, etc.), specifying the Swagger JSON endpoint. app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1")); } app.UseRouting(); //This is for ip //https://stackoverflow.com/a/41335701/1235448 //app.UseForwardedHeaders(new ForwardedHeadersOptions //{ // ForwardedHeaders = ForwardedHeaders.All //}); app.UseAuthentication(); app.UseAuthorization(); app.UseRequestLocalization(o => { o.DefaultRequestCulture = new RequestCulture(Language.English); o.SupportedUICultures = o.SupportedCultures = Language.SystemSupportLanguage().Select(s => (CultureInfo)s).ToList();// SupportedCultures; o.RequestCultureProviders.Clear(); o.RequestCultureProviders.Add(new FrymoCultureProvider()); o.RequestCultureProviders.Add(new QueryStringRequestCultureProvider()); o.RequestCultureProviders.Add(new FacebookQueryStringRequestCultureProvider()); o.RequestCultureProviders.Add(new CookieRequestCultureProvider()); o.RequestCultureProviders.Add(new AuthorizedUserCultureProvider()); o.RequestCultureProviders.Add(new CountryCultureProvider()); o.RequestCultureProviders.Add(new AcceptLanguageHeaderRequestCultureProvider()); }); //if (UseAzureSignalR) //{ app.UseAzureSignalR(routes => { routes.MapHub <SbHub>("/SbHub"); routes.MapHub <StudyRoomHub>("/StudyRoomHub"); }); //} //else //{ // //app.UseSignalR(routes => // //{ // // routes.MapHub<SbHub>("/SbHub"); // // routes.MapHub<StudyRoomHub>("/StudyRoomHub"); // //}); //} app.UseMiddleware <ApplicationInsightMiddleware>(); app.UseEndpoints(endpoints => { //routes.MapRoute( // name: SeoTypeString.Question, // template: "question/{id:long}", // defaults: new { controller = "Home", action = "Index" } //); endpoints.MapControllerRoute( name: SeoTypeString.Static, pattern: "{id}", defaults: new { controller = "Home", action = "Index" } ); endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); endpoints.MapFallbackToController("Index", "Home"); }); //app.UseMvc(routes => //{ // routes.MapRoute( // name: SeoTypeString.Question, // template: "question/{id:long}", // defaults: new { controller = "Home", action = "Index" } // ); // //routes.MapRoute( // // name: SeoTypeString.TutorList, // // template: "tutor-list/{id}", // // defaults: new { controller = "Home", action = "Index" } // //); // routes.MapRoute( // name: SeoTypeString.Static, // template: "{id}", // defaults: new { controller = "Home", action = "Index" } // ); // routes.MapRoute( // name: "default", // template: "{controller}/{action}/{id?}", // defaults: new { controller = "Home", action = "Index" }); // routes.MapSpaFallbackRoute( // name: "spa-fallback", // defaults: new // { // controller = "Home", // action = "Index" // }); //}); }