Tring 覆盖父 ASP.NET 控制器时出现 AmbiguousMatchException

问题描述 投票:0回答:0

万能的!

第二天过去了……需要帮助!

我有两个控制器:ASP.NET C# WebAPI 应用程序中的基础DocumentController 和派生InvoiceController**:**

[ApiController]
[Route("Documents")]
public class DocumentController : ControllerBase
{
    [HttpGet]
    public virtual IEnumerable<Document> Get()
    {
        ... //some implementation
    }
}
[ApiController]
[Route("Documents/Invoices")]
public class InvoiceController : DocumentController
{
    [HttpGet]
    public new IEnumerable<Invoice> Get()
    {
        ... //some implementation
    }
}

我需要第一个显示所有文件,第二个只显示发票

DocumentInvoice也是基础和继承的

public class Document { ... }
public class Invoice: Document { ... }

当我在 derived 控制器中使用 override 关键字时,像这样:

public override IEnumerable<Document> Get()

一切正常,除了我需要一份关于 Invoices 的清单,而不是 Documents

但是当我使用 new 关键字时,像这样:

public new IEnumerable<Invoice> Get()

我得到一个错误:

AmbiguousMatchException:请求匹配多个端点。匹配: CreditController.Get DocumentsController.Get

这意味着,我有两种方法 = 一种来自父“DocumentController”,另一种来自子“InvoiceController”

可以通过给子控制器添加一些路由属性来解决,但是那样很丑

尝试使用不同的路由属性等等...

c# asp.net-core-webapi asp.net-web-api-routing
© www.soinside.com 2019 - 2024. All rights reserved.