使用属性路由接受.NET Core 2.0中API的多个参数

时间:2018-05-26 20:22:25

标签: c# asp.net-core asp.net-core-webapi

我有一个传入的GET请求如下:

https://localhost:44329/api/scoInfo?activityId=1&studentId=1&timestamp=1527357844844

如何在ASP.net-core中创建一个接受此请求的端点。

我尝试了以下不同版本,但没有任何作用:

[HttpGet("{activityId: int}/{studentid: int}/{timestamp: int}")]
[Route("api/ScoInfo/{activityId}/{studentid}/{timestamp}")]

3 个答案:

答案 0 :(得分:2)

使用[FromQuery]指定要应用的确切绑定源。

//GET api/scoInfo?activityId=1&studentId=1&timestamp=1527357844844
[HttpGet("api/scoinfo")] 
public async Task<IActionResult> GetLearningTask(
    [FromQuery]int activityId, 
    [FromQuery]int studentId, 
    [FromQuery]int timeStamp) {
    //...
}
  

MVC将尝试按名称将请求数据绑定到操作参数。 MVC将使用参数名称及其公共可设置属性的名称来查找每个参数的值。

参考Model Binding in ASP.NET Core

答案 1 :(得分:1)

查询参数不是路径路径的一部分。 https://localhost:44329/api/scoInfo?activityId=1&studentId=1&timestamp=1527357844844的路线是&#34; api / scoinfo&#34; 。然后处理方法将使用[FromUri]修饰的参数将映射到查询参数中的值。

[Route("api/scoinfo")]
public HttpResponseMessage GetScoInfo([FromUri]int activityId, int studentId, int timeStamp)

答案 2 :(得分:0)

进一步

注册HKEY_CLASSES_ROOT\<UrlScheme>\shell\open\command

使用(默认)命令= "<path to your app>" "%1"

<UrlScheme>类似于yourapp

这意味着在Windows中的任何地方(通常是从浏览器导航中)都存在一个URL,例如yourapp:<yourdatahere>

位于<path to your app>的应用程序已启动

在命令行中,整个 yourapp:<yourdatahere>通过注册表项中的"%1"传递到启动的应用程序

缺点是要在注册表的该部分设置<UrlScheme>,您需要提升的访问权限。

使用UAP应用程序有更好的方法,因此对于旧版应用程序(WPF,WinForms等)则更多

HTH