简体   繁体   English

如何通过webapi url将多个值传递给代码?

[英]how to pass multiple values to code behind from webapi url?

I'm trying to pass multiple values from the url using WebApi , but getting some errors. 我正在尝试使用WebApi从URL传递多个值,但出现一些错误。

Currently, I can pass a single value using the code below: 目前,我可以使用以下代码传递单个值:

[HttpGet]
public String PostAction([FromUri] string name)
{
    return "Post Action";
}

How can I achieve it? 我该如何实现? I need the url format also, any help is appreciated 我也需要url格式,任何帮助表示赞赏

With WebAPI, when you use FromUri that means it is coming from the query string. 对于WebAPI,当您使用FromUri ,这意味着它来自查询字符串。 Adding another FromUri argument to your function marked with HttpGet will read another parameter of that name from the query string. 向标有HttpGet的函数中添加另一个FromUri参数将从查询字符串中读取该名称的另一个参数。 So if you make a request to http://localhost/mycontroller/myaction?myFirstParam=firstParamValue&mySecondPar=secondParamValue , that will map the matching query string values to the parameters of your endpoint. 因此,如果您请求http://localhost/mycontroller/myaction?myFirstParam=firstParamValue&mySecondPar=secondParamValue ,它将把匹配的查询字符串值映射到端点的参数。

public String MyAction([FromUri] string myFirstParam, [FromUri] string mySecondParam)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM