Solved

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

Posted on 2016-07-23
5
54 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

752 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