How to add swagger to ASP.NET Core Web API areas controllers - Stack Overflow

admin2025-05-01  0

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.

Share Improve this question edited Jan 3 at 4:31 Shareef DotNet Guru asked Jan 2 at 14:52 Shareef DotNet GuruShareef DotNet Guru 452 bronze badges 3
  • Do you mean, you want to add Swagger UI into your MVC application? Per my understanding, Area is always used in MVC or razor page applications to organize related functionality into a group as a separate, and swagger UI is used for testing web api which doesn't require Area normally. – Tiny Wang Commented Jan 3 at 3:04
  • Hi, Controllers are placed in two different folders, I grouped controllers as per functionality. Areas is a folder name. – Shareef DotNet Guru Commented Jan 3 at 4:29
  • Thanks for your update, I had a test in my side by creating a Test folder and create an API inside it. It worked well too. You can see my screenshot. Pls make sure you've ingrated swagger correctly, and make sure your API controller has unique [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
Add a comment  | 

1 Answer 1

Reset to default 0

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.

转载请注明原文地址:http://anycun.com/QandA/1746111545a91827.html