[英]C# MVC HttpGet add parameters to route
In my controller I have action like this:在我的 controller 我有这样的动作:
[Route("api/[controller]")]
[ApiController]
public class ManageOPIdentifierController : ControllerBase
{
[HttpGet("[action]")]
public OPIdentifiersVM Get(int pageSize, int pageNumber)
{
How to add parameters pageSize and pageNumber to HttpGet?如何将参数pageSize和pageNumber添加到HttpGet? Because now when I have second method Get without parameters I get error because there are two routes with the same definition.
因为现在当我有第二个不带参数的方法 Get 时,我得到错误,因为有两个具有相同定义的路由。 How should looks the first HttpGet route?
第一个 HttpGet 路由应该怎么看?
[HttpGet("[action]/{pageSize}&{pageNumber}")]
Code above doesn't work上面的代码不起作用
Edit: My question has been misunderstood.编辑:我的问题被误解了。 I have two methods Get:
我有两种方法获取:
[HttpGet("[action]")]
public OPIdentifiersVM Get(int pageSize, int pageNumber)
and和
[HttpGet("[action]")]
public List<OPIdentifierVM> Get()
There is no problem to read values from parameters pageSize and pageNumber.从参数 pageSize 和 pageNumber 读取值没有问题。 The problem is that I have two methods with the same Http("[action]").
问题是我有两个具有相同 Http("[action]") 的方法。 And I get error:
我得到错误:
AmbiguousMatchException: The request matched multiple endpoints. Matches:
ManageUuidWeb.Controllers.ManageOPIdentifierController.Get (OneProjectIdentifier.Web)
ManageUuidWeb.Controllers.ManageOPIdentifierController.Get (OneProjectIdentifier.Web)
If I good understood the comment I have to change name one of the method.如果我很好理解评论,我必须更改其中一种方法的名称。 But I want to know if it is possible to have two methods with the same name but with different parameters?
但我想知道是否可以有两个方法名称相同但参数不同?
[HttpGet("[action]")]
public OPIdentifiersVM Get([FromUri] int pageSize, [FromUri] int pageNumber)
(or [FromRoute]
if you are using asp.net core) then it will be accessed by (或
[FromRoute]
如果您使用的是 asp.net 内核),那么它将被
http://localhost/api/ManageOPIdentifier/Get/10/1
or use query parameters for that (it would be better solution)或为此使用查询参数(这将是更好的解决方案)
[HttpGet("[action]")]
public OPIdentifiersVM Get([FromQuery] int pageSize, [FromQuery] int pageNumber)
then然后
http://localhost/api/ManageOPIdentifier/Get?pageSize=10&pageNumber=1
also you can use simple [HttpGet]
when your action name equals to http verb method name当您的操作名称等于 http 动词方法名称时,您也可以使用简单的
[HttpGet]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.