Multiple ApiControllers for ASP.NET Core Web API

Viewed 38

I have been trying to figure out how to use multiple [Route("[Controller]")] attributes in my two controller files.

From what I have looked it up it seems possible, and even MS docs say you can apply multiple [ApiController] attributes by either creating a custom class which has the attribute applied to it, or by adding it as an assembly item in Program.cs which I have also tried.

But no matter when, when I visit my localhost, I get an error from Swagger. Am I misunderstanding something?

Edit: to add, if I change one of the Route attributes to Route("[Action]") it works. Which I don't want the action names to be apart of the endpoint.

//LicensesController.cs
namespace Api.Controller;
using Microsoft.AspNetCore.Mvc;
using Api.Authorization;
using Api.Models;
using Api.Services;

[Authorize]
//[ApiController]
[Route("[Controller]")]
public class LicenseController : ApiController
{
    private readonly LicenseServices _licenseServices;

    // Parameterless constructor: because MS says so. I can't find a reason why that makes sense
    public LicenseController(LicenseServices licenseServices)
    {
        _licenseServices = licenseServices;
    }

    [HttpGet]
    public ActionResult<List<PartialLicense>> Licenses([FromQuery] LicenseQuery query)
    {
        List<PartialLicense> licenses = _licenseServices.Query(query);

        if(!licenses.Any())
            return NotFound();

        return licenses;
    }
}

// UsersController.cs
namespace Api.Controller;

using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Api.Authorization;
using Api.Utils;
using Api.Models.Users;
using Api.Services;

[Authorize]
//[ApiController]
[Route("[Controller]")]
public class UsersController : ApiController
{
    private readonly IUserService _userService;
    private readonly IMapper _mapper;
    private readonly AppSettings _appSettings;

    public UsersController(IUserService userService, IMapper mapper, IOptions<AppSettings> appSettings)
    {
        _userService = userService;
        _mapper = mapper;
        _appSettings = appSettings.Value;
    }

    [AllowAnonymous]
    [HttpPost("authenticate")]
    public IActionResult Authenticate(AuthenticateRequest model)
    {
        var response = _userService.Authenticate(model);
        return Ok(response);
    }
}

//ApiControllerAttribute.cs
namespace Api.Controller;
using Microsoft.AspNetCore.Mvc;

[ApiController]
public class ApiController : ControllerBase
{

}
1 Answers

I figured it out, it was a silly mistake. I had not given the [HttpGet] attributes an endpoint name for the request to be called from.

Related