2

The phrasing of my question may be flawed but please know that I am new to C# and REST and I only have experience writing basic scripts.

My goal is to create a service that allows a client to consume it and access basic arithmetic functions.

My logic (which is probably wrong) is: Create a class called Sum (or whatever the operation is), implement the class on the Controller within an HTTP Post method and return the expected value to the client. Is this correct?

Here is the simple sum class:

 public class Sum { public double value1 { get; set;} public double value2 { get; set;} public double Process(double value1, double value2) { double result = value1 + value2; return result; } } 

Here is the controller method which gives me an error because it cannot convert the double variable to IHTTPActionResult.

 public class CalculatorController : ApiController { public IHttpActionResult Post(double val1, double val2) { Sum sum = null; sum.value1 = val1; sum.value2 = val2; double result = sum.Procesar(val1, val2); return result; } } 

Based on my brief experience I cannot think on a different way to implement the class on the Controller method, is there any better way to accomplish my goal? References to other sites and documentation are appreciated as well.

Thank you.

1 Answer 1

1

as a solution it's possible to return double, you need also to instantiate your class Sum otherwise you will have an error during execution .

You will find the code below :

public class CalculatorController : ApiController { public double Post(double val1, double val2) { Sum sum = new Sum(); sum.value1 = val1; sum.value2 = val2; double result = sum.Process(val1, val2); return result; } } 
Sign up to request clarification or add additional context in comments.

4 Comments

Hi, thank you for your reply. If the actual method is a double, will it be recognized as an HTTP resource? Trying it on Postman gives me a 404.
of course , would add your postman request
{ "value1":"2", "value2":"3" } Is just a json value, the response is ""MessageDetail": "No action was found on the controller 'Calculadora' that matches the request.""
it's normal because it based on your action name, or if you want you can add http verb and actionname annotation .

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.