Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 73
  • Last Modified:

How to use more than one parameter for HttpGet (MVC/WebAPI)

I need to change my WebApi (asp.net Core) controller to add one special GET action that accepts more than 1 parameter. Instead of the regular stuff:
        
        [HttpGet("{id}")]
        public string Get(int id)
        {
            //var result = rep.GetAccount(id);
            return "Regular way hit";
        }

Open in new window


I need to send in multiple parameters or a dto containing those parameters. So basically something like this:
        
[HttpGet("{dto}")]
        public string Get(AccountDTO dto)
        {
            //var result = rep.GetAccount(dto.AccountNumber, dto.AccountName, dto.BrokerName, dto.Symbol);
            return "Hit it";
        }

Open in new window


I can't figure out if this should be done by adding attributes to the HttpGet, or if I need a special route (and if so how to do that).

TIA!
Dennis
0
Dennis_Gundersen
Asked:
Dennis_Gundersen
  • 3
1 Solution
 
käµfm³d 👽Commented:
Add the FromUri attribute to your parameter.
0
 
Dennis_GundersenAuthor Commented:
Hi! I tried that, but I'm unable to separate the different GET methods, unless I create a completely new route. So if I use any sort of parameterized call, It's always caught by this function:
http://localhost:5000/api/accounts/find/?AccountID=0&AccountNumber=123456&AccountName=DEV&BrokerNameTest%20broker&Symbol=GBPUSD

        [HttpGet("{accountid}")]
        public string Get(int AccountID)
        {
            var result = rep.GetAccountByID(AccountID);
            return "Hit single function";
        }

Open in new window

Even though the URL has "find" in it I never reach the intended action:
        [HttpGet("{dto}")]
        public string Find([FromUri]AccountDTO dto)
        {
            //var result = rep.GetAccount(dto.AccountID, dto.AccountNumber, dto.AccountName, dto.BrokerName, dto.Symbol);
            return "Hit dto function";
        }

Open in new window


If I add a completely new route I can make it work like this:
        [Route("[action]/{accountnumber}/{accountname}/{brokername}/{symbol}")]
        [HttpGet]
        public string Check(int AccountNumber, string AccountName, string BrokerName, string Symbol)
        {
            //http://localhost:5000/api/accounts/check/123456/DEV/Test%20broker/GBPUSD
            var result = rep.CheckAccount(AccountNumber, AccountName, BrokerName, Symbol);
            return "Hit Check function";
        }

Open in new window


This just looks all wrong to me.

Re
Dennis
0
 
Dennis_GundersenAuthor Commented:
I got one step further, by adding the route to my find function, I can finally reach both methods:
        [Route("[action]/{dto}")]
        [HttpGet("{dto}")]
        public string Find(int id, [FromUri]AccountDTO dto)
        {
            //var result = rep.GetAccount(dto.AccountID, dto.AccountNumber, dto.AccountName, dto.BrokerName, dto.Symbol);
            return "Hit dto function";
        }

        [HttpGet("{accountid}")]
        public string Get(int AccountID)
        {
            var result = rep.GetAccountByID(AccountID);
            return "Hit single function";
        }

Open in new window

http://localhost:5000/api/accounts/1 hits Get()
http://localhost:5000/api/accounts/find/1 hits Find()

Find can't translate the variables into a dto, but that probably only means I need to build a client that translates the dto into proper json. Can that be tested with Fiddler or Postman?
Re
Dennis
0
 
Prakash SamariyaIT ProfessionalCommented:
Add parameters name in route
[Route("{id}/{AccountDTO}")]
[HttpGet]
public string Get(int id, AccountDTO dto)
{
     return "Hit single function";
}

//Another old one Get method
[HttpGet("{accountid}")]
public string Get(int AccountID)
{
     var result = rep.GetAccountByID(AccountID);
     return "Hit single function";
}

Open in new window

0
 
Dennis_GundersenAuthor Commented:
Ah, using Route instead of HttpGet. I knew there was a simple solution that I was forgetting. Thanks!
Re
Dennis
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now