参考
.NET WebAPI 基础 FromRoute、FromQuery、FromBody 用法 - 知乎 (zhihu.com)
使用
环境搭建
创建实体类 Parameter

url 传参
使用 【 & 】
请求格式: https://localhost:7290/api/Param?Method=Get&Value=Query `
以 参数 接收

大小写都可以

以 实体类 接收
在接受的实体类前添加 [FromQuery]

以 【 Rest风格 】
以 参数 接受
注意:
若传入参数与接收参数不一致,可使用[FromRoute(Name = "xxx")]
绑定
参考: .net 请求参数与调用参数不一致 - 掘金 (juejin.cn)

csharp
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace Parameter.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class Param : ControllerBase
{
[HttpGet("{Method}/{Value}")]
public async Task<Parameter> GetRouteAsync([FromRoute(Name = "Method")] string method, [FromRoute(Name = "Value")] string value)
{
Parameter parameter = new Parameter
{
Method = method,
Value = value
};
return await Task.FromResult(parameter);
}
}
}
以 实体类 接收
注意 :
- {xxx} 中的字母
大小
写要与实体类中属性一致
- 在实体类前添加
[FromRoute]
即可


请求体 传参
注: 若以参数接受,请求参数将会暴露在请求url上

以 实体类 接受

在接收的实体类前加[FromBody]
csharp
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace Parameter.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class Param : ControllerBase
{
[HttpPost]
public async Task<Parameter> PostAsync([FromBody] Parameter body)
{
return await Task.FromResult(body);
}
}
}