0

I have an API which would return data using this format:

http://etr.azurewebsites.net/api/calculation/calculation?currency=eur&edition=standart&systems=50&version=5

I want to create an HttpActionResult which looks lie below:

public class CalculateController : ApiController
    {
        // GET: Calculate
        [HttpGet]
        public IHttpActionResult CalculatePrice([FromUri]string currency,
                                                [FromUri]string edition = null,
                                                [FromUri]int? systems = null,
                                                [FromUri]string version = null)
        {

            //Code here

        }
    }

My RouteConfig class looks like:

public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { action = "Index", id = UrlParameter.Optional }
            );
        }

Also the Controller from where i get the data looks like this:

public class CalculationController : ApiController
{
    [HttpGet]
    public async Task<IHttpActionResult> Calculate(.._parameters_..)
    {
        //Code here...
    }
}

How could i make a request to get the data from the URL i want?

8
  • basepath/api/Calculation/Calculate?param=values Commented Jul 21, 2016 at 9:30
  • Why do you need to go via API calls, if you have access to both the Calculation Logic and Calculate logic/Code, are the in the same solution? Commented Jul 21, 2016 at 9:48
  • No they are not in the same solution, they are in different ones. @PreetSingh Commented Jul 21, 2016 at 9:56
  • But you got access to the code how about a common Library. Trying to save a network call Commented Jul 21, 2016 at 9:58
  • @PreetSingh i suggested the same but i got as response that i should be getting data using a Request Commented Jul 21, 2016 at 10:00

1 Answer 1

3

Use the Httpclient to make a request to the url you wanted to call with the right parameters. I guess the url that you want to call is a web api/ RESTful web service.

using (var client = new HttpClient())
{
    // New code:
    client.BaseAddress = new Uri("Your API URL");
    client.DefaultRequestHeaders.Accept.Clear();
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
} 

Based on the type of API method ( GET/ POST ) you will make the appropriate call. You can find more information in this sample.

http://www.asp.net/web-api/overview/advanced/calling-a-web-api-from-a-net-client

the sample has a console application as a client to make call the web api whereas in your case your MVC application is the client.

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

1 Comment

Glad that it helped.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.