Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/308.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 POST-404_C#_Visual Studio 2013_Asp.net Mvc 5_Asp.net Web Api2 - Fatal编程技术网

C# 未找到Web API POST-404

C# 未找到Web API POST-404,c#,visual-studio-2013,asp.net-mvc-5,asp.net-web-api2,C#,Visual Studio 2013,Asp.net Mvc 5,Asp.net Web Api2,在我的VS2013 Web应用程序中,我有许多类似的控制器: [Route("Test/{param1}")] public bool Test (Int32 Param1) { return true; } 我从客户端调用该方法: response = await client.GetAsync("TestCtrlClass/Test/1"); HttpClient client = new HttpClient(); client.BaseAddress = ServerU

在我的VS2013 Web应用程序中,我有许多类似的控制器:

[Route("Test/{param1}")]
public bool Test (Int32 Param1)
{   
  return true;  
}
我从客户端调用该方法:

response = await client.GetAsync("TestCtrlClass/Test/1");
HttpClient client = new HttpClient();
client.BaseAddress = ServerUri;
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

MediaTypeFormatter jsonFormatter = new JsonMediaTypeFormatter();
HttpContent content = new ObjectContent<ClassName>(Item, jsonFormatter);
response = await client.PostAsync("TestCtrlClass/Test2", content);
一切都很好

现在,我需要将一个对象传递给方法,所以我把它放在:

[HttpPost]
[Route("Test2/{item}")]
public bool Test2([FromBody] ClassName item)
{
     return true;       
}
我从客户端调用该方法:

response = await client.GetAsync("TestCtrlClass/Test/1");
HttpClient client = new HttpClient();
client.BaseAddress = ServerUri;
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

MediaTypeFormatter jsonFormatter = new JsonMediaTypeFormatter();
HttpContent content = new ObjectContent<ClassName>(Item, jsonFormatter);
response = await client.PostAsync("TestCtrlClass/Test2", content);
为什么?


谢谢。

请注意,您发送的POST请求正文中包含参数。这意味着您要发布到的URl是
TestCtrlClass/Test2
,而不是
TestCtrlClass/Test2/此处的任何内容。因此,您的属性应该是:

[HttpPost]
[Route("Test2")]
public bool Test2([FromBody] ClassName item)
而且我相信这里不需要
[FromBody]