避免“请求匹配多个动作导致歧义”。 ASP.Net Core中的错误 [英] Avoiding "Request matched multiple actions resulting in ambiguity" error in ASP.Net Core

查看:244
本文介绍了避免“请求匹配多个动作导致歧义”。 ASP.Net Core中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试做一些简单琐碎的事情-或至少是我想的。

I am trying to do something simple and trivial - or at least I thought.

我是试图编写一个基类,该基类可以由我启动的每个微服务项目继承。该基类的重点是测试从HTTP一直到SQL的连通性。

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();
    }
}

我希望将其用作:

[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:多个动作匹配。以下操作匹配路由数据并满足所有约束条件:

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)

MyNamespace.Providers.WebApi.Features.DevTest.DevTestController.Get (MyNamespace.Providers.WebApi) MyNamespace.Infrastructure.Web.Controllers.DevTestControllerBase.Get (MyNamespace.Infrastructure.Web)

而且由于我想使用Swagger,因此在尝试到达Swagger端点时也会出现此错误:

And since I wanted to use Swagger, I am also getting this error when trying to hit the Swagger endpoint:


执行请求
System.NotSupportedException:行为不明确的HTTP方法-
MyNamespace.Providers.WebApi.Features.DevTest.DevTestController.Get
(MyNamespace.Providers.WebApi)。动作需要Swagger 2.0的显式HttpMethod
绑定

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


推荐答案

使您的基本控制器抽象。否则,它们将作为可能的控制器参与路由。老实说,虽然我有些惊讶,但是基于控制器名称约定的路由仍然可以用于以 ControllerBase 结尾的类,而不仅仅是 Controller ,似乎ASP.NET Core会将它们都视为名为 DevTest ,因此模棱两可。

Make your base controllers abstract. Otherwise, they participate in routing as possible controllers that can be routed to. Although I'm a little surprised, honestly, that the routing based on controller name convention still works with a class ending in ControllerBase instead of just Controller, it would appear that ASP.NET Core sees them both as named DevTest, and therefore ambiguous.

您可能还可以将基本控制器重命名为 BaseDevTestController 之类的名称(即,在控制器之前为 Base ) ,然后将其命名为 DevTest BaseDevTest ,从而消除歧义。但是,最好还是将其抽象化,因为无论如何应该如此。您不希望有人真正能够直接导航到您的基本控制器。

You could probably alternatively rename the base controller(s) to something like BaseDevTestController (i.e. with "Base" before "Controller"), which would then make the names DevTest and BaseDevTest, removing the abmiguity. However, it's still a better idea to just make it abstract, as it should be anyways. You wouldn't want someone to actually be able to navigate directly to your base controller(s).

这篇关于避免“请求匹配多个动作导致歧义”。 ASP.Net Core中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆