[英]how to pass a argument with HTTP get method in .net core web api?
I'm trying to build a simple web api using .net core web api which will do basic mathematics operations. 我正在尝试使用.net核心网络api构建一个简单的网络api,该API会执行基本的数学运算。 I have written controller part which consists of multiple get methods when it is called it returns the value with the operation performed.
我编写了控制器部分,该部分由多个get方法组成,当调用它时,它会通过执行的操作返回值。 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. 因此,如何通过api传递参数值,使其返回数学运算的值。 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
就像如果我去https:// localhost:44309 / api / math / add / {{参数值value1和值2分别说25和25}},它将同样返回50 https:// localhost:44309 / api / math /减法/ {{参数值value1和value 2说25和25}},它将返回0
You can pass argument with same url https://localhost:44309/api/math/add?value1=25&value2=25
or change the route to 您可以使用相同的URL传递参数
https://localhost:44309/api/math/add?value1=25&value2=25
或将路由更改为
[HttpGet("Add/{value1}/{value2}")]
and then https://localhost:44309/api/math/add/25/25
然后
https://localhost:44309/api/math/add/25/25
查询字符串参数可以很好地完成此工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.