Program.cs 1.5 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. //Äîáàâëåíèå ãåíåðàòîðà Swagger
  7. builder.Services.AddSwaggerGen(option =>
  8. {
  9. option.SwaggerDoc("v1", new OpenApiInfo { Title = "Ðàçðàáîòêà API", Version = "1.0.0" });
  10. option.TagActionsBy(api =>
  11. {
  12. if (api.GroupName != null)
  13. {
  14. return new[] { api.GroupName };
  15. }
  16. var controllerActionDiscriotor = api.ActionDescriptor as ControllerActionDescriptor;
  17. if (controllerActionDiscriotor != null)
  18. {
  19. return new[] { controllerActionDiscriotor.ControllerName };
  20. }
  21. throw new InvalidOperationException("Unable to determine tag for endpoint.");//Íå óäàëîñü îïðåäåëèòü òåã äëÿ êîíå÷íîé òî÷êè.
  22. });
  23. option.DocInclusionPredicate((name, api) => true);
  24. });
  25. var app = builder.Build();
  26. //Óñòàíàâëèâàåì ñîïîñòàâëåíèå ìàðøðóòîâ ñ êîíòðîëëåðàìè
  27. app.MapControllerRoute(
  28. name: "default",
  29. pattern: "{controller=Home}/{action=Index}/{id?}");
  30. app.UseCors(builder => builder.AllowAnyOrigin()); //Ïîäêëþ÷àåì CORS
  31. //Ðàçðåøåíèå èñïîëüçîâàíèÿ ÏÎ äëÿ îáñëóæèâàíèÿ ñîçäàííîãî äîêóìåíòà JSON è ïîëüçîâàòåëüñêîãî èíòåðôåéñà Swagger
  32. app.UseSwagger();
  33. app.UseSwaggerUI(options =>
  34. {
  35. options.SwaggerEndpoint("./v1/swagger.json", "1.0.0"); //Îòíîñèòåëüíûé ïóòü äî ïîëüçîâàòåëüñêîãî èíòåðôåéñà
  36. });
  37. app.Run();