Solved

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

Posted on 2016-07-23
5
57 Views
Last Modified: 2016-07-26
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
Comment
Question by:Dennis_Gundersen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 41725961
Add the FromUri attribute to your parameter.
0
 

Author Comment

by:Dennis_Gundersen
ID: 41725977
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
 

Author Comment

by:Dennis_Gundersen
ID: 41725992
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
 
LVL 10

Accepted Solution

by:
Prakash Samariya earned 500 total points
ID: 41729455
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
 

Author Comment

by:Dennis_Gundersen
ID: 41729468
Ah, using Route instead of HttpGet. I knew there was a simple solution that I was forgetting. Thanks!
Re
Dennis
0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question