0

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
{

}
4
  • What error are you getting? Swashbuckle might not support the same things that ASP.NET supports. i.e. multiple Route attributes may be supported by ASP.NET, but it might confuse Swashbuckle. Commented Sep 7, 2022 at 17:34
  • What are you hoping to accomplish? What would a Route at the class level mean for an action method in the class? Are you hoping to have two routes to the same action? Commented Sep 7, 2022 at 17:35
  • 1
    No what I was trying to achieve was my api having /api/Users/ and api/Licenses I guess maybe I am still having a misunderstanding regarding what is going on? I thought with the ApiController attribute and then doing Route("[controller]") it would name the route based of the prefix before controller. At least that is how I interpreted it. Commented Sep 7, 2022 at 17:38
  • 1
    I actually just figured it out. I am going answer the question. It was quite silly, and I was overlooking the [HttpGet] attribute on the methods in the license class. I needed to specify the endpoint name for the get because it was happy. Commented Sep 7, 2022 at 17:42

1 Answer 1

0

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.

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.