Swagger returns error when climbing the API

Asked

Viewed 45 times

0

I’m catching this mistake:

Fetch errorundefined https://localhost:44358/Swagger/v2/Swagger.json

When I only had one controller, Swagger would go up normally and I could do my tests and so on. Now, by adding the second controller, it started to give this error. My controllers have the same method names, just pointing to their respective Model. For example, to delete use Delete, to pick up data only, I have Get() and Get(int id), on all controllers these are the names. I thought that might be it. All controllers are decorated with Route("api/[controller]"). Man startup in the method ConfigureService have it

services.AddSwaggerGen(options =>
{
    options.SwaggerDoc("v2", new Microsoft.OpenApi.Models.OpenApiInfo
    {
        Title = "Minha API",
        Version = "v2",
        Description = "Teste de admissão",
    });
});

And yet in the Startup in the Configure method

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{            
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
        app.UseSwagger();
        app.UseSwaggerUI(c =>
        {
            c.SwaggerEndpoint("v2/swagger.json", "Minha API V2");
        });
    }

    app.UseRouting();

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllerRoute(
           name: "defaut",
           pattern: "{controller}/{action}/{id?}");
    });

    app.UseSwagger();
    app.UseSwaggerUI(options => options
        .SwaggerEndpoint("/swagger/v2/swagger.json", "Minha API"));
}

And in my Launchsettings I have:

"profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "swagger",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "MinhaApi": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "swagger",
      "applicationUrl": "https://localhost:5001;http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
}

I really don’t know what to do to fix this mistake

The controllers(Developer)

[AllowAnonymous]
    [Route("api/[controller]")]
    public class DesenvolvedorController : Controller
    {
        private readonly MeuContext _context;
        private DesenvolvedorService _service;

        //Paginação
        public async Task<IActionResult> Index(string filter, int pageindex = 1, string sort = "Nome")
        {
            var resultado = _context.Desenvolvedores.AsNoTracking()
                                        .AsQueryable();

            if (!string.IsNullOrWhiteSpace(filter))
            {
                resultado = resultado.Where(p => p.Nome.Contains(filter));
            }
            var model = await PagingList.CreateAsync(resultado, 5, pageindex, sort, "Nome");
            model.RouteValue = new RouteValueDictionary { { "filter", filter } };
            return View(model);
        }

        public DesenvolvedorController(DesenvolvedorService service, MeuContext  context)
        {
            _service = service;
            _context = context;
        }

        [HttpGet]
        public IEnumerable<Desenvolvedor> Get()
        {
            return _service.Get();
        }

        [HttpGet("{id}")]
        public IActionResult Get(int id)
        {
            var desenvolvedor = _service.Get(id);
            if (desenvolvedor != null)
                return new ObjectResult(desenvolvedor);
            else
                return NotFound();
        }
        [HttpPost]
        public void Post([FromBody] Desenvolvedor desenvolvedor)
        {
            _service.Post(desenvolvedor);
        }

        [HttpPut]
        public Resultado Put([FromBody] Desenvolvedor desenvolvedor)
        {
            return _service.Put(desenvolvedor);
        }

        [HttpDelete]
        public Resultado Delete(int id)
        {
            return _service.Delete(id);
        }

And the Project controller

[AllowAnonymous]
    [Route("api/[controller]")]
    public class ProjetoController : Controller
    {
        private readonly MeuContext _context;
        private ProjetoService _service;

        //Paginação
        public async Task<IActionResult> Index(string filter, int pageindex = 1, string sort = "Nome")
        {
            var resultado = _context.Desenvolvedores.AsNoTracking()
                                        .AsQueryable();

            if (!string.IsNullOrWhiteSpace(filter))
            {
                resultado = resultado.Where(p => p.Nome.Contains(filter));
            }
            var model = await PagingList.CreateAsync(resultado, 5, pageindex, sort, "Nome");
            model.RouteValue = new RouteValueDictionary { { "filter", filter } };
            return View(model);
        }
        public ProjetoController(ProjetoService service, MeuContext context)
        {
            _service = service;
            _context = context;
        }

        [HttpGet]
        public IEnumerable<Projeto> Get()
        {
            return _service.Get();
        }

        [HttpGet("{id}")]
        public IActionResult Get(int id)
        {
            var projeto = _service.Get(id);
            if (projeto != null)
                return new ObjectResult(projeto);
            else
                return NotFound();
        }
        [HttpPost]
        public void Post([FromBody] Projeto projeto)
        {
            _service.Post(projeto);
        }

        [HttpPut]
        public Resultado Put([FromBody] Projeto projeto)
        {
            return _service.Put(projeto);
        }

        [HttpDelete]
        public Resultado Delete(int id)
        {
            return _service.Delete(id);
        }
    }
  • can also post both controllers?

  • @novic, I posted the two controllers, as you asked me.

  • hasn’t shown anything yet

  • The problem is with paging. When I commented on both controllers, Swagger worked.

  • I did that and it rolled: [HttpGet("paginacao/{id}")]

  • @novic, there was an error here and it did not go up the edition. I thought it had gone up, but it did not go up. Sorry there.

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.