0

I'm trying to build simple web api using .net core for doing basic calculations like addition, subtraction, multiplication and division. I created controller class with multiple GET methods. controller class code as shown below

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace WebApplication2.Controllers
{
    [Route("api/[controller]")]
    public class OwnerController : Controller
    {

        [HttpGet]
        public int Add(int value1, int value2)
        {
            return value1 + value2;
        }

        [HttpGet]
        public int Substract(int value1, int value2)
        {
            return value1 - value2;
        }

        [HttpGet]
        public int Multiply(int value1, int value2)
        {
            return value1 * value2;
        }

        [HttpGet]
        public int Divide(int value1, int value2)
        {
            return value1 / value2;
        }

        [HttpGet]
        public string Get()
        {
            return "default";
        }

    }
}

How can I route this controller to do specific action when the respective API are called ?

2
  • Please describe the routes you would like to use for each action. Commented Oct 6, 2018 at 17:48
  • 1
    @KirkLarkin If i request for add,sub,mul and div operation by passing values from a client side then server api respond with respective get method Commented Oct 6, 2018 at 18:24

2 Answers 2

1

Your routes are specified at controller level with your

[Route("api/[controller]")]

This template only provides the route to the controller, expecting the framework to figure out the rest of the routing by the Http verbs/methods:

GET api/owner
POST api/owner
PUT api/owner

and so forth. Since you are reusing the GET method, you probably want to add the action name to your route. You can accomplish this in two ways:

  1. Add the action name as part of your route template on the controller
  2. Add the action name to the individual routes by specifying it with the verb

Example 1:

[Route("api/[controller]/[action]")]

Example 2:

[Route("api/[controller]")]
public class OwnerController : Controller
{
    [HttpGet("Add")]
    public int Add(int value1, int value2)
    {
        return value1 + value2;
    }
    [HttpGet("Subtract")]
    public int Substract(int value1, int value2)
    {
        return value1 - value2;
    }
    ...
}

After either of these changes, you can make calls like

GET api/owner/add
GET api/owner/subtract

and so forth.

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

Comments

0

You can use as following

    [HttpGet("addition")]
    public int Add(int value1, int value2)
    {
        return value1 + value2;
    }

then the route for addition is GET api/owner/addition

Comments

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.