Advertisement
TopFloorSolutions

Custom Startup.cs

Aug 20th, 2017
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.04 KB | None | 0 0
  1. using Microsoft.AspNetCore.Builder;
  2. using Microsoft.AspNetCore.Hosting;
  3. using Microsoft.AspNetCore.Localization;
  4. using Microsoft.AspNetCore.Mvc;
  5. using Microsoft.AspNetCore.Rewrite;
  6. using Microsoft.Extensions.Configuration;
  7. using Microsoft.Extensions.DependencyInjection;
  8. using System.Globalization;
  9.  
  10. namespace StartupDemo
  11. {
  12.     #region Class :: Boost.
  13.     /// <summary>
  14.     /// Class: Boost.cs
  15.     /// Abstract: Serves as the core class of the applications startup directives and features, provides container storage for services, and application pathway for launch.
  16.     /// </summary>
  17.     public class Boost
  18.     {
  19.         #region Properties
  20.         public IConfiguration configObj { get; private set; } = null;
  21.         #endregion
  22.  
  23.         #region Methods
  24.         public Boost(IHostingEnvironment host)
  25.         {
  26.             var configObject = new ConfigurationBuilder()
  27.                 .SetBasePath(host.ContentRootPath)
  28.                 .AddJsonFile("config.json")
  29.                 .AddEnvironmentVariables();
  30.  
  31.             configObj = configObject.Build();
  32.         }
  33.  
  34.         /// <summary>
  35.         /// Service container called by the runtime to provide servics.
  36.         /// </summary>
  37.         /// <param name="services"></param>
  38.         public void ConfigureServices(IServiceCollection services)
  39.         {
  40.             // MVC
  41.             services.AddMvc();
  42.             services.AddCors(opts =>
  43.             {
  44.                 opts.AddPolicy("Policy", policyBuilder =>
  45.                 {
  46.                     policyBuilder.WithOrigins("https://backdoorsecurity.io");
  47.                 });
  48.             });
  49.             services.AddMemoryCache();
  50.             services.AddSession();
  51.  
  52.             services.Configure<MvcOptions>(opt =>
  53.             {
  54.                 opt.Filters.Add(new RequireHttpsAttribute());
  55.             });
  56.  
  57.         }
  58.  
  59.  
  60.         /// <summary>
  61.         /// Application pipeline HTTP request pipeline that is called by the runtime to handle incoming HTTP requests...
  62.         /// </summary>
  63.         /// <param name="app"></param>
  64.         public void Configure(IApplicationBuilder app)
  65.         {
  66.  
  67.             var culturesSupported = new[] { new CultureInfo("en-US") };
  68.  
  69.             var options = new RewriteOptions()
  70.                     .AddRedirectToHttps();
  71.             app.UseRewriter(options);
  72.  
  73.             app.UseRequestLocalization(new RequestLocalizationOptions
  74.             {
  75.                 DefaultRequestCulture = new RequestCulture("en-US"),
  76.                 SupportedCultures = culturesSupported,
  77.                 SupportedUICultures = culturesSupported
  78.             });
  79.  
  80.             app.UseDeveloperExceptionPage();
  81.             app.UseDatabaseErrorPage();
  82.  
  83.             app.UseSession();
  84.             app.UseStaticFiles();
  85.  
  86.  
  87.             app.UseMvc(route =>
  88.             {
  89.                 route.MapRoute(
  90.                     name: "default",
  91.                     template: "{controller=Home}/{action=Index}/{id?}"
  92.                     );
  93.             });
  94.         }
  95.  
  96.         #endregion
  97.     }
  98.     #endregion
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement