Solved

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

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

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

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…
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…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

707 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

20 Experts available now in Live!

Get 1:1 Help Now