Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将多个参数传递给web api_C#_Json_Asp.net Web Api - Fatal编程技术网

C# 如何将多个参数传递给web api

C# 如何将多个参数传递给web api,c#,json,asp.net-web-api,C#,Json,Asp.net Web Api,我正在用fiddler测试我的请求 我使用下面的reuest调用我的web api方法..它工作正常 http://localhost:50079/Import/Test/abc Type :Get web api method: [ActionName("Test")] public bool getconnection(string id) { return true; } 如果我传递多个参数,我

我正在用fiddler测试我的请求

我使用下面的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未找到

我过去喜欢:

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 }
);
试试上面的

是更优雅的解决方案

但是,如果不想使用任何路由,则必须将附加参数作为查询字符串参数传递,因为路由引擎不知道要映射到哪些变量的值(默认路由中配置的
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配置为按操作名称路由。请参阅。

您将HttpGet属性放在方法上,如下所示

//http://localhost:50079/api/Import/abc?value=cde
[HttpGet]
[ActionName("Test")] 
public bool getconnection(string id,string value)   
{
    return true;   
}

[FromBody]一个参数和[FromUri]一个参数。 例如:

[FromBody]=>ajax数据 [FromUri]=>查询字符串数据


但解决办法在这里

我已从Fiddler传递…它给出错误..HTTP/1.1 404 Not FoundTot,如果我的参数有空格b/w单词,我如何传递值。比如:Biz-sight(在URL中)您需要用“%20”替换的空格字符,比如:我已经回答了POST和GET请求的通用解决方案。
public bool InserOrUpdate([FromBody] User user,[FromUri] IsNew)