Startup.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using BlazorStrap;
  2. using HyperCube.Areas.Identity;
  3. using HyperCube.Data;
  4. using Microsoft.AspNetCore.Builder;
  5. using Microsoft.AspNetCore.Components;
  6. using Microsoft.AspNetCore.Components.Authorization;
  7. using Microsoft.AspNetCore.Hosting;
  8. using Microsoft.AspNetCore.HttpsPolicy;
  9. using Microsoft.AspNetCore.Identity;
  10. using Microsoft.AspNetCore.Identity.UI;
  11. using Microsoft.EntityFrameworkCore;
  12. using Microsoft.Extensions.Configuration;
  13. using Microsoft.Extensions.DependencyInjection;
  14. using Microsoft.Extensions.Hosting;
  15. using System;
  16. using System.Collections.Generic;
  17. using System.Linq;
  18. using System.Threading.Tasks;
  19. using Console = HyperCube.Utils.AdvConsole;
  20. namespace HyperCube
  21. {
  22. public class Startup
  23. {
  24. public Startup(IConfiguration configuration)
  25. {
  26. Configuration = configuration;
  27. Models.AccountModel.InitializeAccounts();
  28. try
  29. {
  30. Console.WriteLine("OnInitializedAsync RegisterNetworks");
  31. Models.Blockchain.RegisterNetworks();
  32. }
  33. catch (Exception e)
  34. {
  35. Console.WriteLine(e.Message + ", stack trace:" + e.StackTrace);
  36. }
  37. Console.WriteLine($"Paths, app: [{AppDomain.CurrentDomain.BaseDirectory}], working: [{Environment.CurrentDirectory}]");
  38. }
  39. public IConfiguration Configuration { get; }
  40. // This method gets called by the runtime. Use this method to add services to the container.
  41. // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
  42. public void ConfigureServices(IServiceCollection services)
  43. {
  44. string connection;
  45. #if DEBUG
  46. connection = "DevConnection";
  47. #else
  48. connection = "ProdConnection";
  49. #endif
  50. services.AddDbContext<ApplicationDbContext>(options =>
  51. options.UseMySQL(
  52. Configuration.GetConnectionString(connection)));
  53. services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
  54. .AddRoles<IdentityRole>()
  55. .AddEntityFrameworkStores<ApplicationDbContext>();
  56. services.AddRazorPages();
  57. services.AddServerSideBlazor();
  58. services.AddScoped<AuthenticationStateProvider, RevalidatingIdentityAuthenticationStateProvider<IdentityUser>>();
  59. services.AddDatabaseDeveloperPageExceptionFilter();
  60. services.AddMvc(options => options.EnableEndpointRouting = false).SetCompatibilityVersion(Microsoft.AspNetCore.Mvc.CompatibilityVersion.Version_3_0);
  61. services.AddScoped<AppData>();
  62. services.AddBootstrapCss();
  63. }
  64. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  65. public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  66. {
  67. if (env.IsDevelopment())
  68. {
  69. app.UseDeveloperExceptionPage();
  70. app.UseMigrationsEndPoint();
  71. }
  72. else
  73. {
  74. app.UseExceptionHandler("/Error");
  75. // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
  76. app.UseHsts();
  77. }
  78. app.UseHttpsRedirection();
  79. app.UseStaticFiles();
  80. //app.UseStaticFiles(new StaticFileOptions
  81. //{
  82. // ServeUnknownFileTypes = true
  83. //});
  84. app.UseRouting();
  85. app.UseMvcWithDefaultRoute();
  86. app.UseAuthentication();
  87. app.UseAuthorization();
  88. app.UseEndpoints(endpoints =>
  89. {
  90. endpoints.MapControllers();
  91. endpoints.MapBlazorHub();
  92. endpoints.MapFallbackToPage("/_Host");
  93. });
  94. }
  95. }
  96. }