[英]how to pass mult parameters to web api
我正在使用小提琴手测试我的请求..
我使用下面的reuest来调用我的web api方法..它工作正常。
http://localhost:50079/Import/Test/abc
Type :Get
web api method:
[ActionName("Test")]
public bool getconnection(string id)
{
return true;
}
如果我传递多个参数我收到错误: HTTP / 1.1 404 Not Found
我用过:
http://localhost:50079/Import/Test/abc/cde
Type :Get
web api method:
[ActionName("Test")]
public bool getconnection(string id,string value)
{
return true;
}
我不想使用任何路线...让我知道为什么如果我传递多个参数为什么它不被识别..
您必须指定匹配的路线
config.Routes.MapHttpRoute(
name: "TestRoute",
routeTemplate: "api/{controller}/{id}/{value}",
defaults: new { id = RouteParameter.Optional, value = RouteParameter.Optional }
);
试试以上
你把HttpGet属性放在方法上,像这样?
//http://localhost:50079/api/Import/abc?value=cde
[HttpGet]
[ActionName("Test")]
public bool getconnection(string id,string value)
{
return true;
}
TGH的答案是更优雅的解决方案。
但是,如果您不想使用任何路由,则必须将其他参数作为查询字符串参数传递,因为路由引擎不知道要将哪些值映射到哪些变量(默认情况下配置的id
参数除外)路线)。
基于Web API约定,如果您有这样的控制器:
public class ImportController : ApiController
{
[ActionName("Test")]
public bool GetConnection(string id, string value)
{
return true;
}
}
相应的URI将是:
http://localhost:50079/api/Import/abc?value=cde
如果要映射以使用[ActionName]
属性,则需要将API配置为按操作名称进行路由。 请参阅本教程 。
[FromBody]一个参数和[FromUri]一个参数。 例:
public bool InserOrUpdate([FromBody] User user,[FromUri] IsNew)
[FromBody] => ajax data [FromUri] => QueryString数据
但解决方案就是这样 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.