Startup.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using Microsoft.AspNetCore.Builder;
  2. using Microsoft.AspNetCore.Hosting;
  3. using Microsoft.Extensions.Configuration;
  4. using Microsoft.Extensions.DependencyInjection;
  5. using Microsoft.OpenApi.Models;
  6. using System.Reflection;
  7. using ReZero;
  8. using ReZero.SuperAPI;
  9. using SqlSugar;
  10. namespace ServiceCenter.WebApi
  11. {
  12. public class Startup
  13. {
  14. public Startup(IConfiguration configuration)
  15. {
  16. Configuration = configuration;
  17. }
  18. public IConfiguration Configuration { get; }
  19. public string MyCors = "Cor";
  20. // This method gets called by the runtime. Use this method to add services to the container.
  21. public void ConfigureServices(IServiceCollection services)
  22. {
  23. services.AddControllers();
  24. //跨域配置
  25. services.AddCors(v => v.AddPolicy(MyCors, y =>
  26. {
  27. //声明跨域策略:允许所有域、允许任何请求头和允许全部http方法
  28. y.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod();
  29. }));
  30. services.AddSwaggerGen(c =>
  31. {
  32. //c.SwaggerDoc("v1", new OpenApiInfo { Title = "WCSAPI", Version = "v1" });
  33. c.SwaggerDoc("v1", new OpenApiInfo
  34. {
  35. Version = "v1",
  36. Title = "WCSAPI",
  37. Description = "API描述"
  38. });
  39. var xmlFilename = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
  40. c.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, xmlFilename));
  41. });
  42. //注册ReZero.Api
  43. services.AddReZeroServices(api =>
  44. {
  45. var apiObj = new SuperAPIOptions();
  46. //Swagger地址
  47. apiObj.UiOptions.DefaultIndexSource = "/Swagger";
  48. apiObj.DatabaseOptions = new DatabaseOptions()
  49. {
  50. ConnectionConfig = new SuperAPIConnectionConfig()
  51. {
  52. ConnectionString = "datasource=xxx.db",
  53. DbType = DbType.Sqlite
  54. }
  55. };
  56. //启用超级API
  57. api.EnableSuperApi(apiObj);
  58. });
  59. }
  60. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  61. public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  62. {
  63. app.UseDeveloperExceptionPage();
  64. app.UseSwagger();
  65. app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "WebApplication1 v1"));
  66. //http://localhost:8080/swagger/index.html
  67. app.UseHttpsRedirection();
  68. app.UseRouting();
  69. app.UseCors(MyCors);
  70. app.UseAuthorization();
  71. app.UseEndpoints(endpoints =>
  72. {
  73. endpoints.MapControllers();
  74. });
  75. }
  76. }
  77. }