Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using Microsoft.AspNetCore.Builder;
- using Microsoft.AspNetCore.Hosting;
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.HttpsPolicy;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.AspNetCore.SpaServices.Webpack;
- using Microsoft.Extensions.Configuration;
- using Microsoft.Extensions.DependencyInjection;
- using System.Reflection;
- using Agility.Web;
- using Website.Helpers;
- using Microsoft.Extensions.DependencyInjection.Extensions;
- using Microsoft.Net.Http.Headers;
- namespace Website
- {
- public class Startup
- {
- public Startup(IConfiguration configuration)
- {
- Configuration = configuration;
- }
- public IConfiguration Configuration { get; }
- // This method gets called by the runtime. Use this method to add services to the container.
- public void ConfigureServices(IServiceCollection services)
- {
- services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
- var assembly = typeof(Startup).GetTypeInfo().Assembly;
- services.AddMvc()
- .AddApplicationPart(assembly)
- .AddControllersAsServices();
- AgilityContext.ConfigureServices(services, Configuration);
- }
- // 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();
- app.UseBrowserLink();
- }
- else
- {
- app.UseExceptionHandler("/Home/Error");
- }
- app.UseStaticFiles(new StaticFileOptions()
- {
- ServeUnknownFileTypes = true,
- OnPrepareResponse = context =>
- {
- context.Context.Response.Headers["Strict-Transport-Security"] = "max-age=31536000; includeSubDomains";
- context.Context.Response.Headers["X-Frame-Options"] = "SAMEORIGIN";
- context.Context.Response.Headers["X-XSS-Protection"] = "1; mode=block";
- context.Context.Response.Headers["X-Permitted-Cross-Domain-Policies"] = "none";
- context.Context.Response.Headers[HeaderNames.CacheControl] = "public, max-age=1296000,max-age=31536000, must-revalidate";
- }
- });
- app.Use(async (context, next) =>
- {
- context.Response.Headers["Strict-Transport-Security"] = "max-age=31536000; includeSubDomains";
- context.Response.Headers["X-Frame-Options"] = "SAMEORIGIN";
- context.Response.Headers["X-XSS-Protection"] = "1; mode=block";
- context.Response.Headers["X-Permitted-Cross-Domain-Policies"] = "none";
- await next();
- if (context.Response.StatusCode == 404 && !context.Response.HasStarted)
- {
- //Re-execute the request so the user gets the error page
- string originalPath = context.Request.Path.Value;
- context.Items["originalPath"] = originalPath;
- context.Request.Path = "/page-not-found";
- await next();
- }
- });
- app.UseMvc(routes =>
- {
- //Agility Builtin Route
- routes.MapRoute("Agility", "{*sitemapPath}", new { controller = "Agility", action = "RenderPage" },
- new { isAgilityPath = new Agility.Web.Mvc.AgilityRouteConstraint() });
- routes.MapRoute(
- name: "default",
- template: "{controller=Home}/{action=Index}/{id?}");
- });
- app.MapWhen(context => context.Request.Path.Value.EndsWith("sitemap.xml", true, null), appBranch =>
- {
- appBranch.UseSitemapHandler();
- });
- app.MapWhen(context => context.Request.Path.Value.EndsWith("robots.txt", true, null), appBranch =>
- {
- appBranch.UseCustomPageHandler();
- });
- //configure the Agility Context
- AgilityContext.Configure(app, env, useResponseCaching: true);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment