I am using .NET 6, and I am trying to add Swagger to existing service, controllers are placed in two different folders based on it's functionality, one set of controllers are under Controllers
folder, and the other set is in an Areas
folder.
Swagger is working only for the controllers which there in the Controllers
folder. Areas folder controllers are not showing up in SwaggerUI.
Example:
--Host.web (root folder)
--Areas
--Controllers (Folder 1)
--V1
--set of controllers
--Controllers (Folder 2)
--V1
--set of controllers
Under folder 2 controllers are showing up in SwaggerUI, but areas controllers (in folder 1) are not shown.
How to resolve this? Thanks in advance.
I am using .NET 6, and I am trying to add Swagger to existing service, controllers are placed in two different folders based on it's functionality, one set of controllers are under Controllers
folder, and the other set is in an Areas
folder.
Swagger is working only for the controllers which there in the Controllers
folder. Areas folder controllers are not showing up in SwaggerUI.
Example:
--Host.web (root folder)
--Areas
--Controllers (Folder 1)
--V1
--set of controllers
--Controllers (Folder 2)
--V1
--set of controllers
Under folder 2 controllers are showing up in SwaggerUI, but areas controllers (in folder 1) are not shown.
How to resolve this? Thanks in advance.
First of all, Area should be used for web app like MVC or razor pages which is used to group as a separate Namespace for routing/Folder structure for views and Razor Pages. We can also see scaffold option for Area creation in VS.
In the meantime, APIs should be distinguished by [Route]
and http type, just like what you know as RESTful. Coming back to your topic, I created an .Net 6 MVC app and create Area structure. I have below controllers in Area.
[Area("Admin")]
[Route("admin/api/[controller]")]
[ApiController]
public class HelloController : ControllerBase
{
[HttpGet]
public string hello()
{
return "hello admin area";
}
}
[Area("Admin")]
public class HomeController : Controller
{
public IActionResult Index()
{
return View();
}
}
And below controllers.
[Route("api/[controller]")]
[ApiController]
public class HelloController : ControllerBase
{
[HttpGet]
public string hello()
{
return "hello world";
}
}
public class HomeController : Controller
{
public IActionResult Index()
{
return View();
}
}
Then I followed this document to integrate swagger ui into my app. It requires to install <PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
and add codes below.
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
...
var app = builder.Build();
...
app.UseSwagger();
app.UseSwaggerUI();
Even if we don't put the Controllers inside Area folder, no matter what folder it is, swagger could also work well.
[Route]
definition. For MVC controller, we can have the same controller name and action name, but in API controller, we have to make sure routes are different witth each other by setting Route attribute. You can see my sample codes below. – Tiny Wang Commented Jan 3 at 5:41