I'm trying to build a simple web api using .net core web api which will do basic mathematics operations. I have written controller part which consists of multiple get methods when it is called it returns the value with the operation performed. controller code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
namespace Calculation.Controllers
{
[Route("api/[controller]")]
public class MathController : 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;
}
[HttpGet("Multiply")]
public int Multiply(int value1, int value2)
{
return value1 * value2;
}
[HttpGet("Division")]
public int Division(int value1, int value2)
{
return value1 / value2;
}
}
}
so how can I Pass the argument values with the api so it return the value of mathematical operation. like if I go to https://localhost:44309/api/math/add/ {{argument values value1 and value 2 say 25 and 25}} it will return 50 likewise https://localhost:44309/api/math/subtract/ {{argument values value1 and value 2 say 25 and 25}} it will return 0
You can pass argument with same url https://localhost:44309/api/math/add?value1=25&value2=25
or change the route to
[HttpGet("Add/{value1}/{value2}")]
and then https://localhost:44309/api/math/add/25/25
查询字符串参数可以很好地完成此工作。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.