thebossnt

startup asp.net core with agility

Sep 18th, 2019
560
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.43 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Microsoft.AspNetCore.Builder;
  6. using Microsoft.AspNetCore.Hosting;
  7. using Microsoft.AspNetCore.Http;
  8. using Microsoft.AspNetCore.HttpsPolicy;
  9. using Microsoft.AspNetCore.Mvc;
  10. using Microsoft.AspNetCore.SpaServices.Webpack;
  11. using Microsoft.Extensions.Configuration;
  12. using Microsoft.Extensions.DependencyInjection;
  13. using System.Reflection;
  14. using Agility.Web;
  15. using Website.Helpers;
  16. using Microsoft.Extensions.DependencyInjection.Extensions;
  17. using Microsoft.Net.Http.Headers;
  18.  
  19. namespace Website
  20. {
  21.     public class Startup
  22.     {
  23.         public Startup(IConfiguration configuration)
  24.         {
  25.             Configuration = configuration;
  26.         }
  27.  
  28.         public IConfiguration Configuration { get; }
  29.  
  30.         // This method gets called by the runtime. Use this method to add services to the container.
  31.         public void ConfigureServices(IServiceCollection services)
  32.         {
  33.  
  34.             services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
  35.             var assembly = typeof(Startup).GetTypeInfo().Assembly;
  36.  
  37.             services.AddMvc()
  38.                 .AddApplicationPart(assembly)
  39.                 .AddControllersAsServices();
  40.  
  41.             AgilityContext.ConfigureServices(services, Configuration);
  42.         }
  43.  
  44.         // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  45.         public void Configure(IApplicationBuilder app, IHostingEnvironment env)
  46.         {
  47.             if (env.IsDevelopment())
  48.             {
  49.                 app.UseDeveloperExceptionPage();
  50.                 app.UseBrowserLink();
  51.             }
  52.             else
  53.             {
  54.                 app.UseExceptionHandler("/Home/Error");
  55.             }
  56.  
  57.        
  58.             app.UseStaticFiles(new StaticFileOptions()
  59.             {
  60.                 ServeUnknownFileTypes = true,
  61.                 OnPrepareResponse = context =>
  62.                 {
  63.                     context.Context.Response.Headers["Strict-Transport-Security"] = "max-age=31536000; includeSubDomains";
  64.                     context.Context.Response.Headers["X-Frame-Options"] = "SAMEORIGIN";
  65.                     context.Context.Response.Headers["X-XSS-Protection"] = "1; mode=block";
  66.                     context.Context.Response.Headers["X-Permitted-Cross-Domain-Policies"] = "none";
  67.                     context.Context.Response.Headers[HeaderNames.CacheControl] = "public, max-age=1296000,max-age=31536000, must-revalidate";
  68.                 }
  69.             });
  70.  
  71.  
  72.             app.Use(async (context, next) =>
  73.             {
  74.                 context.Response.Headers["Strict-Transport-Security"] = "max-age=31536000; includeSubDomains";
  75.                 context.Response.Headers["X-Frame-Options"] = "SAMEORIGIN";
  76.                 context.Response.Headers["X-XSS-Protection"] = "1; mode=block";
  77.                 context.Response.Headers["X-Permitted-Cross-Domain-Policies"] = "none";
  78.  
  79.                 await next();
  80.  
  81.                 if (context.Response.StatusCode == 404 && !context.Response.HasStarted)
  82.                 {
  83.                     //Re-execute the request so the user gets the error page
  84.                     string originalPath = context.Request.Path.Value;
  85.                     context.Items["originalPath"] = originalPath;
  86.                     context.Request.Path = "/page-not-found";
  87.                     await next();
  88.                 }
  89.  
  90.             });
  91.  
  92.             app.UseMvc(routes =>
  93.             {
  94.                 //Agility Builtin Route
  95.                 routes.MapRoute("Agility", "{*sitemapPath}", new { controller = "Agility", action = "RenderPage" },
  96.                     new { isAgilityPath = new Agility.Web.Mvc.AgilityRouteConstraint() });
  97.  
  98.                 routes.MapRoute(
  99.                     name: "default",
  100.                     template: "{controller=Home}/{action=Index}/{id?}");
  101.             });
  102.  
  103.             app.MapWhen(context => context.Request.Path.Value.EndsWith("sitemap.xml", true, null), appBranch =>
  104.             {
  105.                 appBranch.UseSitemapHandler();
  106.             });
  107.  
  108.             app.MapWhen(context => context.Request.Path.Value.EndsWith("robots.txt", true, null), appBranch =>
  109.             {
  110.                 appBranch.UseCustomPageHandler();
  111.             });
  112.  
  113.             //configure the Agility Context
  114.             AgilityContext.Configure(app, env, useResponseCaching: true);
  115.         }
  116.     }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment