简体   繁体   中英

C# MVC HttpGet add parameters to route

In my controller I have action like this:

    [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? Because now when I have second method Get without parameters I get error because there are two routes with the same definition. How should looks the first HttpGet route?

[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. The problem is that I have two methods with the same 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM