我正在尝试做某事simple and trivial——或者至少我是这么认为的。
我正在尝试编写一个可以由我启动的每个微服务项目继承的基类。该基类的目的是测试从 HTTP 一直到 SQL 的连接性。它在 PROD 中未启用。
这是(更简单的)基类之一:
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();
}
}
我希望将其用作:
[Produces("application/json")]
[Route("api/DevTest")]
public class DevTestController : DevTestControllerBase
{
public DevTestController(IHostingEnvironment env, IConfiguration configuration, IMediator mediator) : base(env, configuration, mediator) { }
}
不幸的是,它会产生以下错误:
AmbigouslyActionException:匹配多个操作。以下操作与路线数据匹配并满足所有约束:
MyNamespace.Providers.WebApi.Features.DevTest.DevTestController.Get
(MyNamespace.Providers.WebApi)
MyNamespace.Infrastruct.Web.Controllers.DevTestControllerBase.Get
(我的命名空间.基础设施.Web)
由于我想使用 Swagger,因此在尝试访问 Swagger 端点时也收到此错误:
执行请求时发生未处理的异常
System.NotSupportedException:不明确的 HTTP 操作方法 -
MyNamespace.Providers.WebApi.Features.DevTest.DevTestController.Get
(MyNamespace.Providers.WebApi)。操作需要显式 HttpMethod
Swagger 2.0 的绑定