C# 为什么只有在添加正确的主体时,我才能获得404代码来发布到Web API?

C# 为什么只有在添加正确的主体时,我才能获得404代码来发布到Web API?,c#,asp.net-web-api,dotnet-httpclient,C#,Asp.net Web Api,Dotnet Httpclient,我有以下代码: HttpClient client = new HttpClient(); var uri = new Uri("<SERVER_HOST_NAME>/api/message/picture"); Photo p = new Photo { //set four int variables and read an image into a byte[] from a file }; var content = new StringContent(JsonC

我有以下代码:

HttpClient client = new HttpClient();
var uri = new Uri("<SERVER_HOST_NAME>/api/message/picture");

Photo p = new Photo
{
    //set four int variables and read an image into a byte[] from a file
};

var content = new StringContent(JsonConvert.SerializeObject(p), Encoding.UTF8, "application/json");


HttpResponseMessage resp = null;
resp = await client.PostAsync(uri, content);
//do stuff with the response
我正在尝试访问的功能:

[HttpPost, ActionName("picture")]
public void AddPicture([FromBody] Photo p)
{
    //Call two methods on the photo
}

目前,这给了我一个404错误。当我将字符串内容更改为随机字符字符串时,它会找到函数,但返回500,因为随机字符串无法转换为照片。我尝试过使用
MultipartContent
MultipartFormDataContent
这两种方法,结果完全相同。我不知所措,因为我还有其他功能,它们的工作原理与此非常相似,并且工作得非常完美。我做错了什么?

有时会启动404错误以响应代码中的某些异常。你能给我们展示更多的
AddPicture
方法吗?@yorodm它从来没有进入过那个代码。我已经将VisualStudio连接到web服务,并在其中放置了一个断点,但它从未被命中。如果您必须知道,代码调用了两种方法,一种是将图像保存到服务器硬盘上,另一种是将记录插入到SQL数据库中以获取照片。因此,如果您发送错误的正文,它将失败并出现500个错误,而正确的正文将失败并出现错误路由?@yorodm Yes,没错。你愿意把它放在一个简单的项目中复制并上传吗?我觉得你发来的细节无法回答。
[HttpPost, ActionName("picture")]
public void AddPicture([FromBody] Photo p)
{
    //Call two methods on the photo
}