I am trying to do something simple and trivial - or at least I thought.
I am trying to write a base class that can be inherited by each micro-service project I spin up. The point of this base class is to test connectivity from HTTP all the way through to SQL. It is NOT enabled in PROD.
This is one of the (simpler) base classes:
public class DevTestControllerBase: ApiControllerBase
{
public DevTestControllerBase(IHostingEnvironment env, IConfiguration configuration = null, IMediator mediator = null) : base(env, configuration, mediator)
{
}
[HttpGet]
public IActionResult Get()
{
var response = Mediator.Send(new QueryGet());
return Ok(response.Result);
}
[HttpGet("{id}", Name = "Get")]
public IActionResult Get(Guid id)
{
var response = Mediator.Send(new QueryGetById(id));
return Ok(response.Result);
}
[HttpPost]
public async Task<IActionResult> Post([FromBody]DevTestModelBinding value)
{
if (!ModelState.IsValid)
return BadRequest(ModelState);
var response = await Mediator.Send(new CommandPost(value));
return Created("Get", new { id = response });
}
[HttpPut("{id}")]
public IActionResult Put(Guid id, [FromBody]DevTestModelBinding value)
{
if (!ModelState.IsValid)
return BadRequest(ModelState);
Mediator.Send(new CommandPut(id, value));
return Ok();
}
[HttpDelete("{id}")]
public IActionResult Delete(Guid id)
{
Mediator.Send(new CommandDelete(id));
return Ok();
}
}
I was hoping to use it as:
[Produces("application/json")]
[Route("api/DevTest")]
public class DevTestController : DevTestControllerBase
{
public DevTestController(IHostingEnvironment env, IConfiguration configuration, IMediator mediator) : base(env, configuration, mediator) { }
}
Unfortunately, it produces this error instead:
AmbiguousActionException: Multiple actions matched. The following actions matched route data and had all constraints satisfied:
MyNamespace.Providers.WebApi.Features.DevTest.DevTestController.Get (MyNamespace.Providers.WebApi) MyNamespace.Infrastructure.Web.Controllers.DevTestControllerBase.Get (MyNamespace.Infrastructure.Web)
And since I wanted to use Swagger, I am also getting this error when trying to hit the Swagger endpoint:
An unhandled exception has occurred while executing the request System.NotSupportedException: Ambiguous HTTP method for action - MyNamespace.Providers.WebApi.Features.DevTest.DevTestController.Get (MyNamespace.Providers.WebApi). Actions require an explicit HttpMethod binding for Swagger 2.0