Solved

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

Posted on 2016-07-23
5
43 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
  • 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 8

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
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.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

911 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now