VivekDev
VivekDev

Reputation: 25553

Failed to load API definition 500 Internal Server Error with Swagger

I created a brand new Asp.net Core 2.2 web api app. Then I configured Swagger as follows. In the ConfigureServices method of the startup file I have the following.

services.AddSwaggerGen(c =>
{
    c.SwaggerDoc("v1", new Info
    {
        Title = "My API", Version = "v1" 
    });
});

services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

Next in the Configure method, I have,

if (env.IsDevelopment())
    app.UseDeveloperExceptionPage();
else
    app.UseHsts();

app.UseSwagger();
app.UseHttpsRedirection();
app.UseMvc();

app.UseSwaggerUI(c =>
{
    c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
    c.RoutePrefix = string.Empty;
});

So far so good. Swagger UI is working Awesome. Problems started when I introduced a simple MVC controller.

[Route("[controller]/[action]")]
public class HomeController : Controller
{
    public IActionResult Index()
    {
        return View();
    }
}

I get the following error

Failed to load API definition Errors Fetch error Internal Server Error /swagger/v1/swagger.json

Swagger Error with Asp.net Mvc Core Controller

Seems that Swagger is not accepting Asp.net core mvc controller. If I comment out that mvc controller, things work fine again.

Is there a way out of this.

Upvotes: 5

Views: 25490

Answers (3)

you
you

Reputation: 1

if your project module name in go.mod of root path is module my-work/my-go-project, write this

import _ "my-work/my-go-project/docs"

Upvotes: 0

Deepak Singhvi
Deepak Singhvi

Reputation: 787

Your docs are not imported.

Assume your project is github.com/deepaksinghvi/catalogwf and docs folder is is inside it.

try importing your docs as follows and it would work

_ "github.com/deepaksinghvi/catalogwf/docs"

Upvotes: 3

Tarumã Miranda
Tarumã Miranda

Reputation: 81

Try to decorate your method in controller with http verb notation:

[Route("[controller]/[action]")]
public class HomeController : Controller
{
    [HttpGet, Route("myRoute")]
    public IActionResult Index()
    {
        return View();
    }
}

Just the [HttpGet] notation method should solve the problem, if you dont want to put the route.

Upvotes: 4

Related Questions