Program.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using Microsoft.AspNetCore.Mvc.Controllers;
  2. using Microsoft.OpenApi.Models;
  3. var builder = WebApplication.CreateBuilder(args);
  4. builder.Services.AddCors();//Äîáàâëÿåì ñåðâèñû CORS
  5. builder.Services.AddControllers();//Äîáàâëÿåì êîíòðîëëåðû
  6. builder.Services.AddSwaggerGen(option =>
  7. {
  8. option.SwaggerDoc("v1", new OpenApiInfo { Title = "", Version = "v1" });
  9. option.TagActionsBy(api =>
  10. {
  11. if (api.GroupName != null)
  12. {
  13. return new[] { api.GroupName };
  14. }
  15. var controllerActionDescriptor = api.ActionDescriptor as ControllerActionDescriptor;
  16. if (controllerActionDescriptor != null)
  17. {
  18. return new[] { controllerActionDescriptor.ControllerName };
  19. }
  20. throw new InvalidOperationException("Unable to determine tag for endpoint.");
  21. });
  22. option.DocInclusionPredicate((name, api) => true);
  23. });
  24. var app = builder.Build();
  25. //Óñòàíàâëèâàåì ñîïîñòàâëåíèå ìàðøðóòîâ ñ êîíòðîëëåðàìè
  26. app.MapControllerRoute(
  27. name: "default",
  28. pattern: "{controller =Home}/{action=Index}/{id?}");
  29. app.UseCors(builder => builder.AllowAnyOrigin()); //Ïîäêëþ÷àåì CORS
  30. //Ðàçðåøåíèå èñïîëüçîâàíèÿ ÏÎ äëÿ îáñëóæèâàíèÿ ñîçäàííîãî äîêóìåíòà JSON è ïîëüçîâàòåëüñêîãî èíòåðôåéñà Swagger
  31. app.UseSwagger();
  32. app.UseSwaggerUI(options =>
  33. {
  34. options.SwaggerEndpoint("./v1/swagger.json", "v1"); //Îòíîñèòåëüíûé ïóòü äî ïîëüçîâàòåëüñêîãî èíòåðôåéñà
  35. });
  36. app.Run();