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.

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. 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. 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. Commented Jan 3 at 5:41

1 Answer 1

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.

enter image description here

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();

enter image description here

Even if we don't put the Controllers inside Area folder, no matter what folder it is, swagger could also work well.

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.