Program.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. * https://habr.com/ru/articles/513514/
  3. */
  4. using Microsoft.AspNetCore.Mvc.Controllers;
  5. using Microsoft.OpenApi.Models;
  6. namespace APINet
  7. {
  8. public class Program
  9. {
  10. public static void Main(string[] args)
  11. {
  12. var builder = WebApplication.CreateBuilder(args);
  13. builder.Services.AddCors();
  14. builder.Services.AddControllers();
  15. builder.Services.AddSwaggerGen(option =>
  16. {
  17. option.SwaggerDoc("1v", new OpenApiInfo { Title = "API", Version = "1v" });
  18. option.TagActionsBy(api =>
  19. {
  20. if (api.GroupName != null)
  21. {
  22. return new[] { api.GroupName };
  23. }
  24. var controllerActionDescriptor = api.ActionDescriptor as ControllerActionDescriptor;
  25. if (controllerActionDescriptor != null)
  26. {
  27. return new[] { controllerActionDescriptor.ControllerName };
  28. }
  29. throw new InvalidOperationException("Unable to determine tag for endpoint.");
  30. });
  31. option.DocInclusionPredicate((name, api) => true);
  32. });
  33. var app = builder.Build();
  34. app.MapControllerRoute(
  35. name: "default",
  36. pattern: "{controller = Home}/{action=Index}/{id?}");
  37. app.UseCors(builder => builder.AllowAnyOrigin());
  38. app.UseSwagger();
  39. app.UseSwaggerUI(c =>
  40. {
  41. c.SwaggerEndpoint("/swagger/1v/swagger.json", "1v");
  42. c.RoutePrefix = string.Empty;
  43. });
  44. app.Run();
  45. }
  46. }
  47. }