Startup.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. namespace ServiceCenter.WebApi
  8. {
  9. public class Startup
  10. {
  11. public Startup(IConfiguration configuration)
  12. {
  13. Configuration = configuration;
  14. }
  15. public IConfiguration Configuration { get; }
  16. public string MyCors = "Cor";
  17. // This method gets called by the runtime. Use this method to add services to the container.
  18. public void ConfigureServices(IServiceCollection services)
  19. {
  20. services.AddControllers();
  21. //跨域配置
  22. services.AddCors(v => v.AddPolicy(MyCors, y =>
  23. {
  24. //声明跨域策略:允许所有域、允许任何请求头和允许全部http方法
  25. y.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod();
  26. }));
  27. services.AddSwaggerGen(c =>
  28. {
  29. //c.SwaggerDoc("v1", new OpenApiInfo { Title = "WCSAPI", Version = "v1" });
  30. c.SwaggerDoc("v1", new OpenApiInfo
  31. {
  32. Version = "v1",
  33. Title = "WCSAPI",
  34. Description = "API描述"
  35. });
  36. var xmlFilename = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
  37. c.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, xmlFilename));
  38. });
  39. }
  40. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  41. public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  42. {
  43. app.UseDeveloperExceptionPage();
  44. app.UseSwagger();
  45. app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "WebApplication1 v1"));
  46. //http://localhost:8080/swagger/index.html
  47. app.UseHttpsRedirection();
  48. app.UseRouting();
  49. app.UseCors(MyCors);
  50. app.UseAuthorization();
  51. app.UseEndpoints(endpoints =>
  52. {
  53. endpoints.MapControllers();
  54. });
  55. }
  56. }
  57. }