c# - 一起为控制器 .NET 核心 Web API 查询字符串和属性路由
问题描述
我试图实现这样的目标
namespace CoreAPI.Controllers
{
[Route("api/[controller]")]
public class ValuesController : Controller
{
// GET api/values
// GET api/values/5
[HttpGet("{id}")]
public string Get(int id)
{
return "value";
}
[HttpGet]
public string GetValue(string name,string surname)
{
return "Hello " + name;
}
}
}
我想通过使用这两个 URL 来调用这个控制器方法:
解决方案
您可以通过在控制器方法之上定义多个路由来解决此问题
[HttpGet("GetValues")]
[HttpGet("GetValues/{name}/{surname}")]
public string GetValue(string name, string surname)
{
return "Hi" + name;
}
这将适用于http://localhost:11979/api/values/GetValues/John/lawrance
和http://localhost:11979/api/values/GetValues?name=john&surname=lawrance
要添加更多:
[HttpGet]
[Route("GetValues")]
[Route("GetValues/{name}/{surname}")]
public string GetValue(string name,string surname)
{
return "Hello " + name + " " + surname;
}
这也有效。
推荐阅读
- typescript - 索引表达式不是“数字”类型
- javascript - sequelize 验证多列 [this.column got undefined]
- android - 管理 Google Play 订阅不起作用
- keras - “连接”层需要具有匹配形状的输入,连接轴除外
- javascript - Lua表到js数组
- c# - Stimulsoft 中的 Parent Key 和 ChildKey 是相同的
- html - document.getElementById 在 Vue2 组件中未定义......因为滥用手表?
- python - 线性回归器无法预测一组值;错误:ValueError:形状(100,1)和(2,1)未对齐:1(dim 1)!= 2(dim 0)
- azure - Azure Active Directory B2C 自定义邀请策略 - 在步骤之间传递自定义声明
- r - 如何使用 nlme 的 varFunc 对残差进行建模?