如何在ASP.NET Core Web API中使用相同数量的参数重载控制器方法? [英] How to overload controller methods with same number of arguments in ASP.NET Core Web API?

查看:721
本文介绍了如何在ASP.NET Core Web API中使用相同数量的参数重载控制器方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将完整的.NET Framework Web API 2 REST项目迁移到ASP.NET Core 2.2,并且在路由中有些丢失。

I'm migrating a full .NET Framework Web API 2 REST project over to ASP.NET Core 2.2 and getting a bit lost in the routing.

在Web中API 2我能够根据参数类型使用相同数量的参数重载路由,例如我可以有 Customer.Get(int ContactId) Customer.Get(DateTime includeCustomersCreatedSince),传入的请求将被路由

In Web API 2 I was able to overload routes with the same number of parameters based on the parameter type, e.g. I could have Customer.Get(int ContactId) and Customer.Get(DateTime includeCustomersCreatedSince) and incoming requests would be routed accordingly.

我无法在.NET Core中实现相同的功能,或者出现405错误或404错误,而出现此错误:

I haven't been able to achieve the same thing in .NET Core, I either get a 405 error or a 404 and this error instead:


{\ error\:\该请求匹配了多个端点。匹配项:\r\n\r\n [AssemblyName] .Controllers.CustomerController.Get([AssemblyName])\r\n [AssemblyName] .Controllers.CustomerController.Get([AssemblyName])\ }

"{\"error\":\"The request matched multiple endpoints. Matches: \r\n\r\n[AssemblyName].Controllers.CustomerController.Get ([AssemblyName])\r\n[AssemblyName].Controllers.CustomerController.Get ([AssemblyName])\"}"

这是我完整的.NET Framework应用程序Web API 2应用程序中的工作代码:

This was working code in my full .NET framework app Web API 2 app:

[RequireHttps]    
public class CustomerController : ApiController
{
    [HttpGet]
    [ResponseType(typeof(CustomerForWeb))]
    public async Task<IHttpActionResult> Get(int contactId)
    {
       // some code
    }

    [HttpGet]
    [ResponseType(typeof(List<CustomerForWeb>))]
    public async Task<IHttpActionResult> Get(DateTime includeCustomersCreatedSince)
    {
        // some other code
    }
}

这就是我在Core 2.2中将其转换为的内容:

And this is what I converted it to in Core 2.2:

[Produces("application/json")]
[RequireHttps]
[Route("api/[controller]")]
[ApiController]
public class CustomerController : Controller
{
    public async Task<ActionResult<CustomerForWeb>> Get([FromQuery] int contactId)
    {
        // some code
    }

    public async Task<ActionResult<List<CustomerForWeb>>> Get([FromQuery] DateTime includeCustomersCreatedSince)
    {
        // some code
    }
}

如果我注释掉 Get 方法之一,则上面的代码有效,但是当我有两个时,上述代码就会失败获取方法。我期望 FromQuery 可以在请求中使用参数名称来控制路由,但事实并非如此?

The code above works if I comment out one of Get methods, but fails as soon as I have two Get methods. I'd expected the FromQuery to use the parameter name in the request to steer the routing, but that doesn't seem to be the case?

是否可以重载像这样的控制器方法,其中您具有相同数量的参数,并且可以根据参数的类型或名称进行路由?

Is it possible to overload a controller method like this where you have the same number of parameters and either route based on the parameter's type or the parameter's name?

推荐答案

您不能执行操作重载。路由在ASP.NET Core中的工作方式与在ASP.NET Web Api中的方式不同。但是,您可以简单地组合这些操作,然后在其中分支,因为所有参数都是可选的:

You cannot do action overloads. The way routing works in ASP.NET Core is different than how it did in ASP.NET Web Api. However, you can simply combine these actions and then branch inside, since all params are optional:

public async Task<ActionResult<CustomerForWeb>> Get(int contactId, DateTime includeCustomersCreatedSince)
{
    if (contactId != default)
    {
        ...
    }
    else if (includedCustomersCreatedSince != default)
    {
        ...
    }
}

这篇关于如何在ASP.NET Core Web API中使用相同数量的参数重载控制器方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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