Startup.cs 2.3 KB

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