0

I am building a webapi to getBalance of a customer from Db.It works well and i can retrieve the data.But the problem is in the parameter part.

For eg. In ASP.NET webservice when we request the service it gives us a page where according to the the service we get to enter the parameters in the textbox and upon

firing up the service we get the data.

I have my webapi up and running and the uri looks like this --- http://localhost/api/accounts/balance/cs-001

AccountsController

public class AccountsController : ApiController
{

    [HttpGet]
    [ActionName("balance")]
    public string Getbalance(string accountNumber)
    {
        var data = BusinessLayer.Api.AccountHolderApi.GetBalance(accountNumber);

        return data;
    }

}

And my route

RouteTable.Routes.MapHttpRoute("OfficeApi", "api/{controller}/{action}/{accountNumber}");

So the accountNumber is the paramater here.Now one of my mobile app dev friend to is going to use this api suggests me to move account number in parameter rather than url.Like in my case we append the parameter in the url to retrieve the balance from the db.I want to know how to move the account number (the param) from the url to parameter in asp.net webapi.

I am new to webapi.Help needed.Thank You.

2 Answers 2

2

All you have to do is change the route since account number is already a parameter on the function:

RouteTable.Routes.MapHttpRoute("OfficeApi", "api/{controller}/{action}");

Then you'll be able to do:

http://localhost/api/accounts/balance?accountNumber=cs-001

Sign up to request clarification or add additional context in comments.

1 Comment

Wow.thank you sir.I have a small question.Now, to say give my api to the one who wants to use it..should the format be in this order--domain/api/accounts/balance{param}.
1

@Avitus's answer is correct. Here is some belabor in case you are interested.

If someone requests this URL: http://localhost/api/accounts/balance?accountNumber=cs-001

ASP.NET routing will figure out the controller and action based on your routing configuration. Once the controller and action have been discovered, then it will try to bind the parameters in the action like this:

By default, Web API uses the following rules to bind parameters:

  1. If the parameter is a "simple" type, Web API tries to get the value from the URI. Simple types include the .NET primitive types (int, bool, double, and so forth), plus TimeSpan, DateTime, Guid, decimal, and string, plus any type with a type converter that can convert from a string. (More about type converters later.)
  2. For complex types, Web API tries to read the value from the message body, using a media-type formatter.

Here is the full article.

1 Comment

Thank you for the belabor part sir.:)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.