locked
Need help on Rest calls RRS feed

  • Question

  • User1133338985 posted

    Hello All,

    I am having issues on how I can have multiple gets in my restful class, and how to call them.

    I am guessing this would be a routing issue, but I don't know how to setup my routing or how I would call different methods.

    Right now I have 2 methods that both take a string param and return that same param.

     public string GetPriceForCustomer(string id)
    		{
    			try
    			{
                   
    				ConnectionStringSettings connectionString = ConfigurationManager.ConnectionStrings["ERP"];
                   
                    using (SqlConnection cn = new SqlConnection(connectionString.ConnectionString))
    				{
                       
                        using (SqlCommand command = new SqlCommand("salesforce_getProductPrice", cn))
    					{
                         
                            command.CommandType = CommandType.StoredProcedure;                       
                            cn.Open();                       
                            string tmp = command.ExecuteScalar().ToString();
                            return "price is " + tmp.ToString() + " ID is " + id;
                        }
    				}
    			}
    			catch (Exception e)
    			{
    				Trace.WriteLine(e.Message);
                    return "Error in rest " + e.Message;
    				
    			}
    		}
     public string GetPriceForCustomerA(string boo)
    		{
    			try
    			{
                   
    				ConnectionStringSettings connectionString = ConfigurationManager.ConnectionStrings["ERP"];
                   
                    using (SqlConnection cn = new SqlConnection(connectionString.ConnectionString))
    				{
                       
                        using (SqlCommand command = new SqlCommand("salesforce_getProductPrice", cn))
    					{
                         
                            command.CommandType = CommandType.StoredProcedure;                       
                            cn.Open();                       
                            string tmp = command.ExecuteScalar().ToString();
                            return "price is " + tmp.ToString() + " ID is " + boo;
                        }
    				}
    			}
    			catch (Exception e)
    			{
    				Trace.WriteLine(e.Message);
                    return "Error in rest " + e.Message;
    				
    			}
    		}

    How do I set this up to be able to call either one of these methods?

    Thanks for any help or advice,

    KSS

    Friday, September 22, 2017 7:49 PM

Answers

  • User-271186128 posted

    Hi kss,

    How do I set this up to be able to call either one of these methods?

    According to your requirement, I have created a simple demo. You could refer to.
    Code in api Controller:

            [HttpGet]
            [Route("api/Test1/PriceForCustomer/{id}")]
            public string GetPriceForCustomer(string id)
            {
                return "id:" + id;
            }
            [HttpGet]
            [Route("api/Test1/PriceForCustomerA/{boo}")]
            public string GetPriceForCustomerA(string boo)
            {
                return "boo:" + boo;
            }
    

    You could see the result like this:

    Besides, you could refer to this link to define api url routes:

    https://docs.microsoft.com/en-us/aspnet/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 25, 2017 6:29 AM

All replies

  • User1120430333 posted
                

    The method names can be anything you want them to be. The method signatures for passing parms and return types can be any type of primitive or complex object type you want them to be. One can even use method overriding too. 

    http://www.codeguru.com/csharp/.net/using-custom-action-names-in-asp.net-web-api.htm

    <copied>

    Using Attributes to Specify HTTP Method and Action Method Mapping

    You can change the Web API action method names to anything you want and use certain attributes to map them with the HTTP methods. This technique gives you total control on the action method names. The attributes that you use are [HttpGet], [HttpPost], [HttpPut] and [HttpDelete]. As you might have guessed, these attributes correspond to GET, POST, PUT and DELETE HTTP methods. The following code shows how to use these attributes:

    <end>

    Saturday, September 23, 2017 1:44 AM
  • User-271186128 posted

    Hi kss,

    How do I set this up to be able to call either one of these methods?

    According to your requirement, I have created a simple demo. You could refer to.
    Code in api Controller:

            [HttpGet]
            [Route("api/Test1/PriceForCustomer/{id}")]
            public string GetPriceForCustomer(string id)
            {
                return "id:" + id;
            }
            [HttpGet]
            [Route("api/Test1/PriceForCustomerA/{boo}")]
            public string GetPriceForCustomerA(string boo)
            {
                return "boo:" + boo;
            }
    

    You could see the result like this:

    Besides, you could refer to this link to define api url routes:

    https://docs.microsoft.com/en-us/aspnet/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 25, 2017 6:29 AM