First, you can write an extended class to get the AreaName, like this:

  public static class ApiDescriptionExtension
    {
        public static List<string> GetAreaName(this ApiDescription description)
        {
            string areaName = description.ActionDescriptor.RouteValues["Area"];
            List<string> areaList = new List<string>();
            areaList.Add(areaName);
            if (!string.IsNullOrEmpty(areaName))
            {
                description.RelativePath = $"{description.RelativePath}";
            }
            return areaList;
        }
    }

Then in your StartUp(Add this code:c.TagActionsBy(apiDesc => apiDesc.GetAreaName());):

  services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("v1", new OpenApiInfo
            {
               //...
            });

            c.SwaggerDoc("v2", new OpenApiInfo
            {
               //...
            });
            c.TagActionsBy(apiDesc => apiDesc.GetAreaName());

        });

Remember to configure your Area routing:

  app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
            endpoints.MapControllerRoute(
            name: "Area",
            pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}"
            );
        });

In your ApiController in your Area:

Example:

[Area("Area")]
[Produces("application/json")]
[Route("v1/Area/api/[controller]")]
[ApiController]
public class TodoController : ControllerBase
{        
    [HttpGet]
    public ActionResult<List<TodoItem>> GetAll()
    {
        return Ok();
    }

Test result: enter image description here

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top