Startup.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using Autofac;
  2. using Microsoft.AspNetCore.Builder;
  3. using Microsoft.AspNetCore.Hosting;
  4. using Microsoft.AspNetCore.Http;
  5. using Microsoft.Extensions.Configuration;
  6. using Microsoft.Extensions.DependencyInjection;
  7. using Microsoft.Extensions.FileProviders;
  8. using Microsoft.Extensions.Hosting;
  9. using System.IO;
  10. namespace Wicture.DbRESTFul.PMT
  11. {
  12. public class Startup
  13. {
  14. public Startup(IConfiguration configuration)
  15. {
  16. Configuration = configuration;
  17. }
  18. public IConfiguration Configuration { get; }
  19. // This method gets called by the runtime. Use this method to add services to the container.
  20. public void ConfigureServices(IServiceCollection services)
  21. {
  22. services.AddControllersWithViews().AddNewtonsoftJson();
  23. services.AddDbRESTFul();
  24. }
  25. public void ConfigureContainer(ContainerBuilder builder)
  26. {
  27. builder.RegisterDbRESTFul();
  28. }
  29. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  30. public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  31. {
  32. if (env.IsDevelopment())
  33. {
  34. app.UseDeveloperExceptionPage();
  35. }
  36. if (!env.IsDevelopment()) app.UseExceptionHandler("/Home/Error");
  37. else
  38. {
  39. app.UseDeveloperExceptionPage();
  40. }
  41. app.UseStaticFiles();
  42. app.UseCors(x => x.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader());
  43. var docPath = Path.Combine(env.ContentRootPath, "doc/");
  44. if (!Directory.Exists(docPath)) Directory.CreateDirectory(docPath);
  45. app.UseFileServer(new FileServerOptions()
  46. {
  47. FileProvider = new PhysicalFileProvider(docPath),
  48. RequestPath = new PathString("/doc"),
  49. EnableDirectoryBrowsing = false
  50. });
  51. app.UseRouting();
  52. app.UseEndpoints(endpoints =>
  53. {
  54. endpoints.MapControllerRoute(
  55. name: "default",
  56. pattern: "{controller=Home}/{action=Index}/{id?}");
  57. });
  58. app.UseDbRESTFul();
  59. }
  60. }
  61. }