简体   繁体   English

NET Core Web API中如何使用HTTP get方法传递参数?

[英]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.

相关问题 如何将多个参数传递给Get方法并在.NET Core Web API中对它们进行路由? - How to pass multiple parameters to Get Method and Route them in .NET Core Web API? 如何将参数传递给ASP.NET Web API Get方法? - how to pass parameters to asp.net web api get method? 将$ http.get()方法中的$ scope.object传递给asp.net web api - pass $scope.object in $http.get() method to asp.net web api 如何发布到ASP.NET Core Web API控制器并获取带有值的参数 - How to post to asp.net core web api controller and get argument with value 调用.NET Web API GET方法时参数为null - Argument is null in call to .NET Web API GET method 如何在角度$ http.post中传递对象和值,并在ASP .NET Web API中获取它们 - How to pass object and a value in angular $http.post and get those in asp .net web api 如何将数组从Angular 6传递到ASP.NET Core API的GET方法? - How do I pass array from Angular 6 to ASP.NET Core API for GET method? 如何在ASP.NET Core Web API中传递int数组 - How to pass an array of int in ASP.NET Core Web API Asp.net Core Web API中间件错误`“调用”方法的第一个参数必须为“ HttpContext”类型 - Asp.net Core web api middleware Error `The 'Invoke' method's first argument must be of type 'HttpContext'` ASP.NET Core中如何将多个参数传递给一个get方法 - How to pass multiple parameters to a get method in ASP.NET Core
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM